3

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

The text field ,in question, is visible in the window only when you scroll down to see it.

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

So, when the text field is visible in the window the message "blah" is sent to the text field element without any problem.

Instead ,when the text field element is not visible in the window ,because I don't scroll down it, the message "blah" isn't sent to the text field.

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 2
    You can scroll down to the element with selenium as well. http://stackoverflow.com/questions/12293158/page-scroll-up-or-down-in-selenium-webdriver-selenium-2-using-java :P – Fran Montero Jul 06 '15 at 10:41
  • @user3868055 Can u pls share the page link or source – Vicky Jul 06 '15 at 10:53
  • You don't need to scroll down for the webdriver to be able to access an element. The element is 'visible' even if you haven't scrolled to it, so long as it's actually rendered to the dom and not behind another element. But if you actually have to scroll to that part of the page to -render- the element: http://stackoverflow.com/a/12293212/1994255 – Lawrence Dean Vanderpool Jul 06 '15 at 13:20

3 Answers3

0

use executeScript to scroll the element into view.

driver.executeScript("arguments[0].scrollIntoView();",element);
element.sendKeys("blah");
aholt
  • 2,829
  • 2
  • 10
  • 13
0

You can use Actions moveToElement Method for doing things like these

public Actions moveToElement(WebElement toElement)

Moves the mouse to the middle of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.

new Actions(driver).moveToElement(element).build().perform();
element.SendKeys("blah");
Madhan
  • 5,750
  • 4
  • 28
  • 61
0

If you can't set text using standart SendKeys method you can try ExecuteScript (if problem really in SendKeys and not in your selector =)). Smth like:

webdriver.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");
Andrey Egorov
  • 1,249
  • 1
  • 12
  • 9