简体   繁体   中英

How to know if the javascript code has been executed in Selenium Java?

I have this part of the code in my Selenium Webdriver test in java . I use this javascript code to scroll to a particular value i in the page.

((JavascriptExecutor) driver).executeScript("scroll(0,"+i+");");

scroll(0,y) doesn't return a value. Is there any way to know if this script has been executed ?

Did you try getting a return value from the script which then you can handle from Selenium itself? Try using:

if(window.pageYOffset!= undefined){
  return [pageXOffset, pageYOffset];
}

This will return the offsets for you after the scroll. You can then assert if this scroll position is the one you want or not.

Further detail on this approach can be found here:

Getting the return value of Javascript code in Selenium

How to get scrollbar position with Javascript?

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