Scrolling using Selenium WebDriver with Java

前端 未结 8 2056
长情又很酷
长情又很酷 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) + ");");
    }
    

提交回复
热议问题