Selenium sendkeys not working

Good morning, I have a problem in java using the sendKeys command with SELENIUM libraries.

The text box in question only appears in the window when you scroll down to see it.

var element = driver.FindElement(By.Xpath("…"));
element.SendKeys("blah");

      

So, when the textbox is displayed in the window, the message is " blah

" sent to the textbox element without any problem.

Instead, when the textbox element does not appear in the window because I am not scrolling through it, the message " blah

" is not sent to the textbox.

How can I solve this problem? I would like to send a "blah" message to the textbox element also when it doesn't appear in the window. How can i do this?

+3


source to share


3 answers


use executeScript to scroll the item in the view.



driver.executeScript("arguments[0].scrollIntoView();",element);
element.sendKeys("blah");

      

0


source


You can use Actions moveToElement way to do similar actions

public Actions moveToElement (WebElement toElement element)

Move the mouse to the middle of the element. The element is scrolled into the viewport and its location is calculated using the getBoundingClientRect method.



new Actions(driver).moveToElement(element).build().perform();
element.SendKeys("blah");

      

0


source


If you are unable to set the text using the standard SendKeys method, you can try ExecuteScript (if the problem is indeed with SendKeys and not your selector =)). Smth like:

webdriver.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");

      

0


source







All Articles