Scrolling using Selenium WebDriver with Java

前端 未结 8 1924
长情又很酷
长情又很酷 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:53

    You can scroll to the necessary location using javascript You need to use the scrollTo method rather than the scrollBy method for it to work.

    public void scrollToElement(By by) {
        Locatable element = (Locatable) selenium.findElement(by);
        Point p= element.getCoordinates().getLocationOnScreen();
        JavascriptExecutor js = (JavascriptExecutor) selenium;  
        js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()+150) + ");");
    }
    
    0 讨论(0)
  • 2020-12-25 09:53

    Simple use the .sendKeys(Keys.PAGE_DOWN); when your element was visible, just click on it, by .click(element).perform(); for me work something like this:

    clicker = new Actions(driver);
        clicker.sendKeys(Keys.PAGE_DOWN);
        Thread.sleep(1000);
        clicker.click(button).perform();     
        Thread.sleep(1000);
    
    0 讨论(0)
提交回复
热议问题