简体   繁体   中英

Selenium Webdriver : Scroll to the top using Javascript

i'm using Selenium webdriver with javascript & node.js

In some step in my test, i have to automate clicking to a buuton which is in the top of the page.

But as i'm doing some previous treatment , the page automatically scrolls to the bootom , and as a result my button (on the top) is no longer appearing , so i'm catching this error:

Uncaught WebDriverError: unknown error: Element <li>...</li> is not clickable at point (707, 10). Other element would receive the click: <li class="menumain crm-Campaigns" tabindex="11">...</li>

with some googling i fin that i should scroll to the top to make appearing the button .

How i may do it ??

Try below code you will be able to scroll up :

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;

jse.executeScript("window.scrollBy(0,-250)", "");
OR,
jse.executeScript("scroll(0, -250);");

in javascript:

window.scrollTo(0, 0);

This will scroll to the top left corner of your page. But in most cases, this will be equivalent to the top.

Instead of going to the top of the page,get hold of the xpath or id of the button. The code here shows for clicking the button using selenium. WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("document.getElementById('button').click();");

For scrolling use the code below jse.executeScript("window.scrollBy(0,-200)", "");

i did it this way :

driver.executeScript('scroll(0, -250);').then(function() {
    driver.sleep(3000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM