Selenium web driver: cannot be scrolled into view

寵の児 提交于 2019-11-27 02:10:09

Naif,

Actually, your above question is different than the actual question hence you should have asked it as a separate question. Still, I'm answering to your previous question.

The error is because the element you're trying to click on isn't visible. Before you click on element, it should be visible. You can do this by following -

WebElement element = driver.findElement(By.xpath("//div[2]/div[2]"));
WebDriverWait wait = new WebDriverWait(driver, 20); //here, wait time is 20 seconds

wait.until(ExpectedConditions.visibilityOf(element)); //this will wait for elememt to be visible for 20 seconds
element.click(); //now it clicks on element

If above doesn't work, you can click on element by executing javascript(but this isn't a good practice)

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

Try to execute the script and click the element

driver.executeScript("arguments[0].click();", element)

I'm not sure but try and see if following works for you. First, you've to make that element visible before interacting with it -

WebElement element = driver.findElement(By.xpath("//div[2]/div[2]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

The above code will scroll down till the element is visible and then you can click on it.

I was getting this error in slightly different context where I was trying to click anchor tag with the selenium RemoteWebDriver (I was trying to replace WebDriver). The fix was identifying the right set of capability for the driver for eg:

capability = DesiredCapabilities.chrome(); capability.setPlatform(Platform.WIN10); capability.setCapability("version", "66");

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!