scroll up the page to the top in selenium

前端 未结 6 2305
北海茫月
北海茫月 2021-02-19 10:50

How to scroll the webpage to the top of the page.

I know scrolling the page to the bottom is:

window.scrollTo(0,document.body.scrollHeight)
<         


        
6条回答
  •  旧时难觅i
    2021-02-19 11:30

    yes you can try as below

    Way one - Scrolling to bottom of a page

    driver.navigate().to(URL);
    ((JavascriptExecutor) driver)
    .executeScript("window.scrollTo(0, document.body.scrollHeight)");
    

    Way two - Scrolling to an element on a page

    driver.navigate().to(URL);
    WebElement element = driver.findElement(By.id("id"));
            ((JavascriptExecutor) driver).executeScript(
                    "arguments[0].scrollIntoView();", element);
    

    Way 3 -Scrolling by coordinates

     driver.navigate().to(URL);
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
    

提交回复
热议问题