简体   繁体   English

无法使用Selenium WebDriver和Java滚动特定的DIV

[英]Unable to scroll a specific DIV using Selenium WebDriver with Java

WebDriver fails to find the element which is not visible in browser's visible area.In order to make the WebElement viewable by WebDriver, We need to make that element to visible in browser's view to do scroll down on particular div! WebDriver无法找到浏览器可见区域中不可见的元素。为了使WebDri可以查看WebElement,我们需要在浏览器的视图中显示该元素以向下滚动特定的div! I tried lot, doesn't helped me. 我尝试了很多,没有帮助我。 Hence its still not working at all . 因此它仍然没有工作。 Kindly advise 好心提醒

My Code : 我的代码:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//*[@id='lobbyMain']/div[3]/div[2]/ul/li[1]/div[1]/h3/a"))).onclick;

See the basic way to scroll is: 看滚动的基本方法是:

Webelement element = driver.findElement(By.xpath("//*[@id='lobbyMain']/div[3]/div[2]/ul/li[1]/div[1]/h3/a"))

JavascriptExecutor js = (JavascriptExecutor) element;
    int yPosition = element.getLocation().getY();

    for (int second = 0;; second++) {
        if(second >=4){
            break;
        }
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,200)", ""); //y value '400' can be altered
        Thread.sleep(3000);

You can vary seconds as per your convenience. 您可以根据自己的方便来改变秒数。

Or else Just refer to below link. 或者只是参考下面的链接。 It will help you. 它会对你有所帮助。

https://www.seleniumeasy.com/selenium-tutorials/scrolling-web-page-with-selenium-webdriver-using-java https://www.seleniumeasy.com/selenium-tutorials/scrolling-web-page-with-selenium-webdriver-using-java

If you still face problem, then do reply me. 如果你仍然遇到问题,请回复我。 :-) :-)

Selenium will automatically scroll to the element for you when you perform a click on an element. 当您对元素执行单击时,Selenium将自动滚动到该元素。 You can simply do: 你可以简单地做:

driver.findElement(By.xpath("//*[@id='lobbyMain']/div[3]/div[2]/ul/li[1]/div[1]/h3/a")).click()

And selenium should find it, scroll to it and click on it. 硒应该找到它,滚动到它并点击它。 If this does not work, I sometimes use this to scroll: 如果这不起作用,我有时会使用它来滚动:

((JavascriptExecutor) driver).executeScript(
            "scroll(" + element.getLocation().getX() + "," + element.getLocation().getY() + ")");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM