Scrolling using Selenium WebDriver with Java

前端 未结 8 1934
长情又很酷
长情又很酷 2020-12-25 08:47

I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.

<
相关标签:
8条回答
  • 2020-12-25 09:28

    For scrolling down:

    System.setProperty("webdriver.chrome.driver",
                       "/home/shreetesh/chromedriver");
    WebDriver driver = new ChromeDriver(); 
    String url = "https://en.wikipedia.org/wiki/Main_Page";
    driver.get(url);
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("scroll(0, 25000);");
    

    To scrolling up just replace the value of scroll with (2500, 0).

    0 讨论(0)
  • 2020-12-25 09:33

    I recently had this problem due to a Drupal menu blocking the element when I ran this code:

    public void scrollTo(WebElement x) {
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", x);
            }
    

    After referencing this page, I updated to set the boolean to false using this code, and it works great:

    public void scrollTo(WebElement x) {
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", x);
            }
    
    0 讨论(0)
  • 2020-12-25 09:42

    Scroll to top can be done:

    private void scrollToTop() {
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("window.scrollTo(0, 0);");
    }
    
    0 讨论(0)
  • 2020-12-25 09:45
        Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
        int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
        ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
    
    0 讨论(0)
  • 2020-12-25 09:47

    Use below code for scrolling up and scrolling down

    Actions dragger = new Actions(driver);
    
    WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("<Scroll bar Element >"));
    
    // drag downwards
    
    int numberOfPixelsToDragTheScrollbarDown = 50;
    
    for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
        try {
            // this causes a gradual drag of the scroll bar, 10 units at a time
            dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
            Thread.sleep(1000L);
        } catch(Exception e1){}
    } 
    
    // now drag opposite way (downwards)
    numberOfPixelsToDragTheScrollbarDown = -50;
    for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
        // this causes a gradual drag of the scroll bar, -10 units at a time
        dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
        Thread.sleep(1000L);
    }
    
    0 讨论(0)
  • 2020-12-25 09:51

    If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:

    JavascriptExecutor js = (JavascriptExecutor)driver;
                        js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
                        "document.body.scrollHeight,document.documentElement.clientHeight));");
    
    0 讨论(0)
提交回复
热议问题