Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between .SendKeys(Keys.Enter) and .Click()?

I am working with some automation scripts using C# and Selenium, running my tests on IE11. I was trying to click on a link using the .Click() function from WebElement and it wasn't working at all so I tried to use the .SendKeys(Keys.Enter) function and it worked.

I'm trying to understand this behavior but I can't figure out why is this happening.

Does anyone know the difference between using .Click() and .SendKeys(Keys.Enter)?

like image 264
Vinicius Seganfredo Avatar asked Oct 19 '25 08:10

Vinicius Seganfredo


1 Answers

From the documentation

Click()

...If this causes a new page to load, this method will attempt to block until the page has loaded. At this point, you should discard all references to this element and any further operations performed on this element will throw a StaleElementReferenceException...

...There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.

SendKeys()

Use this method to simulate typing into an element, which may set its value.

Both simulate user action on the web element. But while SendKeys() tries to perform the action without consideration to anything else Click() has some safe guards, like "stopping" the code from continue until page load event is received. Also, the method won't try to execute without meeting some preconditions to avoid unexpected behavior.

like image 178
Guy Avatar answered Oct 21 '25 21:10

Guy