Java selenium sendkeys enter

Как нажать Enter в Selenium?

Пишу приложение для автоматизации работы с сайтом. На сайте есть чат, отправка сообщений в который происходит при нажатии кнопки Enter. Без проблем получается найти поле и передать в него значение с помощью .sendKeys(«Text»). Как имитировать нажатие Enter? Пробовал .sendKeys(Key.ENTER). Не работает. Пишу на js.

you_are_enot

Решение оказалось проще, чем предполагалось. Достаточно в конце отправляемого сообщения добавить символ переноса строки «\n»

Нужно понять, почему не работает .sendKeys(Key.ENTER), вполне возможно, что теряется фокус с инпута = неявно теряется курсор. Попробуй указать курсору на место явно. типа такого:
«`
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);
«`
За синтаксис извини, не пишу на js.
Решение костыльное конечно, нужно смотреть почему пропадает фокусировка и переопределить событие sendKeys, например. Но если нужно, так сказать «По быстрому» то должно сработать.

Chefranov

you_are_enot

Войдите, чтобы написать ответ

Эффект плавной прокрутки при переходе на ссылку #якорь?

Источник

Typing Enter/Return key in Selenium.

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys.ENTER as an argument to the method. Also, we can use pass Keys.RETURN as an argument to the sendKeys method for the same purpose.

To use the Keys class, we have to incorporate import org.openqa.selenium.Keys to the code. Let us type Enter/Return after inputting text within the below edit box.

Example

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import org.openqa.selenium.Keys; public class TypeEnter < public static void main(String[] args) < System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // identify element WebElement m=driver.findElement(By.id("gsc−i−id1")); m.sendKeys("Java"); // type enter with sendKeys method and pass Keys.ENTER m.sendKeys(Keys.ENTER); driver.quit(); >>

Example

Code Implementation with Keys.RETURN.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import org.openqa.selenium.Keys; public class TypeReturn < public static void main(String[] args) < System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // identify element WebElement m=driver.findElement(By.id("gsc−i−id1")); m.sendKeys("Java"); // type enter with sendKeys method and pass Keys.RETURN m.sendKeys(Keys.RETURN); driver.quit(); >>

Источник

How to specify «ENTER» button functionality in Selenium WebDriver code?

To specify ENTER button functionality in Selenium webdriver we have to use the method sendKeys. To simulate pressing the ENTER button,we have to add the statement import org.openqa.selenium.Keys to our code.

Then pass the parameter – Keys.RETURN or Keys.ENTER to the sendKeys method.

Let us make an attempt to press the ENTER button after entering some text in the Google search input box −

Example

Code Implementation with Keys.ENTER

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; public class EnterOperation < public static void main(String[] args) < System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().window().maximize(); //URL launch driver.get("https://www.google.com/"); // identify element WebElement e =driver.findElement(By.name("q")); e.sendKeys("Java"); // Keys.ENTER with sendKeys e.sendKeys(Keys.ENTER); >>

Code Implementation with Keys.RETURN

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; public class EnterOperationReturn < public static void main(String[] args) < System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().window().maximize(); //URL launch driver.get("https://www.google.com/"); // identify element WebElement r =driver.findElement(By.name("q")); r.sendKeys("Java"); // Keys.RETURN with sendKeys r.sendKeys(Keys.RETURN); >>

Источник

How to simulate pressing enter in html text input with Selenium?

We can simulate pressing enter in the html text input box with Selenium webdriver. We shall take the help of sendKeys method and pass Keys.ENTER as an argument to the method. Besides, we can pass Keys.RETURN as an argument to the method to perform the same task.

Also, we have to import org.openqa.selenium.Keys package to the code for using the Keys class. Let us press ENTER/RETURN after entering some text inside the below input box.

Example

Code Implementation with Keys.ENTER.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import org.openqa.selenium.Keys; public class PressEnter < public static void main(String[] args) < System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement l=driver.findElement(By.id("gsc-i-id1")); l.sendKeys("Selenium"); // press enter with sendKeys method and pass Keys.ENTER l.sendKeys(Keys.ENTER); driver.close(); >>

Code Implementation with Keys.RETURN.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import org.openqa.selenium.Keys; public class PressReturn < public static void main(String[] args) < System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement l=driver.findElement(By.id("gsc-i-id1")); l.sendKeys("Selenium"); // press enter with sendKeys method and pass Keys.RETURN l.sendKeys(Keys.RETURN); driver.close(); >>

Источник

Читайте также:  JavaScript Email Validation
Оцените статью