简体   繁体   English

如何滚动到 div 元素的底部 Selenium Webdriver

[英]How to scroll to bottom of div element Selenium Webdriver

I have a use case wherein there's a div element on the webpage, it appears like a popup dialog as soon as you click a link (its not an actual popup, its something like dialog boxes which opens in Facebook when you click a link to check reactions on your posts etc.)我有一个用例,其中网页上有一个 div 元素,只要您单击链接,它就会显示为一个弹出对话框(它不是实际的弹出窗口,它类似于在单击链接时在 Facebook 中打开的对话框进行检查)对您的帖子等的反应)

I'm using Selenium WebDriver with Java to automate tests for this application, my use case involves me to scroll to the bottom of the dialog box where there is a link to show more items, when user clicks show more, it populates another 10 items in the list and so on until there are no other items left for the user to visit.我正在使用带有 Java 的 Selenium WebDriver 来自动化此应用程序的测试,我的用例涉及我滚动到对话框底部,那里有一个显示更多项目的链接,当用户点击显示更多时,它会填充另外 10 个项目以此类推,直到没有其他项目可供用户访问。

So basically I have to scroll down on that particular div element till I keep seeing Show More link and when driver is not able to find show more link it should stop.所以基本上我必须向下滚动那个特定的 div 元素,直到我一直看到Show More链接,当驱动程序无法找到 show more 链接时,它应该停止。

Note - I can't just scroll to bottom of the page using javascript window.scrollTo() as it will scroll down through whole webpage, however I just want to scroll to bottom of that division element only.注意 - 我不能使用 javascript window.scrollTo()滚动到页面底部,因为它会向下滚动整个网页,但是我只想滚动到该除法元素的底部。

If anybody have any idea on how to achieve this please let me know.如果有人对如何实现这一目标有任何想法,请告诉我。

Thanks for the help in advance !我在这里先向您的帮助表示感谢 !

Another way of scrolling to the bottom inside (of) any scroll-able element:另一种滚动到任何可滚动元素内部(的)底部的方法:

public void scrollToBottomInsideDiv(WebElement scrollArea) {
    JavascriptExecutor js = ((JavascriptExecutor) webdriver);
    js.executeScript("arguments[0].scrollTo(0, arguments[0].scrollHeight)", scrollArea);
}
WebElement ele= driver.findElement(By.id("id_of_element"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", ele);

or

WebElement ele= driver.findElement(By.id("id_of_element"));
Actions builder = new Actions(driver);
builder.moveToElement(ele).click();
Action build=builder.build();
build.perform();

I have had this kind of issue and I resolved this using a similar question asked on SQA SO.我遇到过这种问题,我使用在 SQA SO 上提出的类似问题解决了这个问题。 Here is the post .这是帖子

Please see the last answer by Sagar007, which uses a separate function scroll_Page() to achieve this.请参阅 Sagar007 的最后一个答案,它使用单独的函数scroll_Page()来实现这一点。

Code taken directly from his answer代码直接取自他的回答

Some HTML page have internal (custom) scroll bar.某些 HTML 页面具有内部(自定义)滚动条。 We have to handle with little bit different way.我们必须以稍微不同的方式处理。 Javascript is not working here. Javascript 在这里不起作用。

Solution :解决方案:

WebElement scrollArea = 
driver.findElement(By.cssSelector("div.slimScrollBar"));

I added a new step, where I clicked on the div element , using click() method and then proceeded to next step.我添加了一个新步骤,在其中单击 div 元素,使用click()方法,然后继续下一步。

Create method scroll_Page as given below.创建方法 scroll_Page 如下所示。 Call this method as将此方法称为

 scroll_Page(scrollArea ,100);

Where scrollArea is your dragged(scroll) element and 100 is scroll points.其中 scrollArea 是您拖动的(滚动)元素,而 100 是滚动点。

  public static boolean scroll_Page(WebElement webelement, int scrollPoints)
{
try
{               
    Actions dragger = new Actions(driver);
    // drag downwards
    int numberOfPixelsToDragTheScrollbarDown = 10;
    for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown)
    {
        dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform();
    }
    Thread.sleep(500);
    return true;
}
catch (Exception e)
{
    e.printStackTrace();
    return false;
}

Use scrollIntoView使用scrollIntoView

scrollIntoView vs moveToElement scrollIntoView vs moveToElement scrollIntoView vs moveToElement scrollIntoView vs moveToElement

Pass the more items link element as an argument.more items链接元素作为参数传递。

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

You can also use moveToElement您还可以使用moveToElement

Actions actions = new Actions(driver); actions.moveToElement(element); actions.perform();

I had a div on a page that needed to be scrolled.我在需要滚动的页面上有一个 div。 Further, the data was loaded dynamically.此外,数据是动态加载的。 The function whould be called from while(someCondition()) loop.该函数应该从while(someCondition())循环中调用。 Used Java使用过Java

    private int scroll; 
    private void scrollDownPopup() {
        scroll += 1000;
        JavascriptExecutor jse = (JavascriptExecutor) browser;
        // you can get query selector from inspector
        jse.executeScript("document.querySelector('body > div.Blah.Blah1 > div > div.ClassOfYourDiv').scrollTo(0, "
                + scroll + ");");
    }

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

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