2

I am using Selenium and java, after clicking on one button I land on another page and I see the input tag that I am looking in the viewport

after waiting for page to load with

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

in order to get the tag I use the scrollIntoView() and search for the element by id using javascript inside java this way:

js.executeScript("document.getElementById('elementId').scrollIntoView(true);");

but the problem is that document.getElementById('elementId') returns null; I tried it also in the firefox webdriver console with the same result.

If I execute document.getElementById('elementId') on the same page using firefox console but without using Selenium webdriver I get the tag as expected.

Why am I getting this null using Selenium? How to fix it?

eeadev
  • 3,662
  • 8
  • 47
  • 100

1 Answers1

3

Please use the below code before the scrollIntoView() code

  driver.switchTo().frame(driver.findElement(By.tagName("iframe")));

IF any element resides under iframe tag you should switch your driver to iframe using above

If you need to switch your driver in default mode then u need to use below code

 driver.switchTo().defaultContent()

If the element is under modal then use it

driver.switchTo().frame("ModelFrameTitle");

or

driver.switchTo().activeElement()
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19