Reputation: 3852
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?
Upvotes: 2
Views: 2032
Reputation: 1165
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()
Upvotes: 3