E
E
Eugene2018-10-24 09:17:07
JavaScript
Eugene, 2018-10-24 09:17:07

How to press Enter in Selenium?

I am writing an application to automate the work with the site. The site has a chat, sending messages to which occurs when you press the Enter button. Without problems, it turns out to find the field and pass the value to it using .sendKeys("Text"). How to simulate pressing Enter? Tried .sendKeys(Key.ENTER). Does not work. I write in js.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2018-10-25
@you_are_enot

The solution turned out to be easier than expected. It is enough to add a line break character "\n" at the end of the sent message

T
timmydiego, 2018-10-24
@timmydiego

It is necessary to understand why .sendKeys(Key.ENTER) does not work, it is quite possible that the focus is lost from the input = the cursor is implicitly lost. Try to point the cursor to the place explicitly. like this:
```
driver.get(' www.example.com ');
var element = driver.findElement(webdriver.By.xpath('//div[yourInputXpath]'));
element.sendKeys('your text is here');
element.click;
element.sendKeys(Keys.ENTER);
```
Sorry for the syntax, I don't write in js.
The solution is a crutch, of course, you need to look at why the focus disappears and redefine the sendKeys event, for example. But if you need to, so to speak "Quick" then it should work.

E
Eugene Chefranov, 2018-10-24
@Chefranov

Try.sendKeys(Key.RETURN)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question