5

As far as I'm aware there are two ways of typing with Selenium:

new Actions(webDriver).sendKeys("text to send").perform(); webElement.sendKeys("text to send"); 

The Actions method seems like the most natural way of replicating a user typing as the keys are sent wherever the browser wants (I believe a method called sendKeysToActiveElement is being used under the covers). However, many tutorials instruct testers to use the WebElement method (this is in fact the only option when using SafariDriver), I assume as it is simpler.

Is the Actions method actually a better simulation of user interaction, or should I use the WebElement method for convenience?

3 Answers 3

5

public Actions sendKeys(java.lang.CharSequence... keys)

Sends keys to the active element. This differs from calling WebElement.sendKeys(CharSequence...) on the active element in two ways:

  1. The modifier keys included in this call are not released.
  2. There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching elements should work.

for more you can refer this link : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html#sendKeys-java.lang.CharSequence...-

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer, this is also the conclusion that I came to.
Glad That I could help !
What is meant by the following? 'The modifier keys included in this call are not released.'
@Afser2000 The statement means that the modifier keys (like Shift, Ctrl) pressed before executing the line still remains pressed and is not released.
2

This doesn't answert the question directly but I would have thought using SendKeys in the WebElement class would have been better, since you already have the WebElement object in memory, why do you need to create an Actions object?

I have always used the WebElement.SendKeys() method and I have not found any uses to switch over to using the Actions class for sending over a regular string.

I would use the Actions class when I require more complex scenarios e.g. Need to hold down a button or drag something.

3 Comments

I agree jamie with you...WebElement.SendKeys() is perfect to use when you want to send only text.
But that involves making an assumption that the element in question would have been the one that the user would have typed in. For example, say I have a test that opens a dialog and types some text in the automatically selected text field. If I use Actions.sendKeys() then the text will only be typed in the field if it is selected, and hence a test can fail if it is not automatically selected. If I use field.sendKeys() then the text will be sent to the field even if it wasn't automatically selected and I won't notice the error.
There is no assumption. The person writing the code should know what they are attempting to do.
1

Two things:

  1. If you want to recreate exactly the interactions of a user, it is better to use Actions, otherwise it is better to use webElement.sendkeys() because it is more practical and simple.
  2. If you need to execute a complex action like drag and drop it is also better to use Actions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.