scroll up the page to the top in selenium

前端 未结 6 2251
北海茫月
北海茫月 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条回答
  •  一生所求
    2021-02-19 11:38

    To scroll to the top of the page, just scroll to the 0, 0:

    window.scrollTo(0, 0);
    

    Or, as an alternative option, you can scroll into view of the header element (or some other element on top):

    WebElement element = driver.findElement(By.tagName("header"));
    
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].scrollIntoView();", element); 
    

提交回复
热议问题