最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Scroll not working in Internet Explorer - Stack Overflow

programmeradmin0浏览0评论

I tried scroll(x,y) using javascript in Internet Explorer 10 . But, it did not work when I tried executing the script for a website. What is the equivalent for the same in IE ? It's a part of a Java Selenium test. I need to scroll in the page. So, I do that by executing a javascript code by using javascript executor.

for(int i=0;i<X;i+=Y)   
String cmd = "window.scrollTo(0,"+i+")";    
((JavascriptExecutor) driver).executeScript(cmd);

I use the above code in my test to scroll the page. But, in Internet Explorer IE10 it doesn't work.

I tried scroll(x,y) using javascript in Internet Explorer 10 . But, it did not work when I tried executing the script for a website. What is the equivalent for the same in IE ? It's a part of a Java Selenium test. I need to scroll in the page. So, I do that by executing a javascript code by using javascript executor.

for(int i=0;i<X;i+=Y)   
String cmd = "window.scrollTo(0,"+i+")";    
((JavascriptExecutor) driver).executeScript(cmd);

I use the above code in my test to scroll the page. But, in Internet Explorer IE10 it doesn't work.

Share Improve this question edited May 23, 2014 at 14:10 anirudh_raja asked May 23, 2014 at 13:56 anirudh_rajaanirudh_raja 3792 gold badges9 silver badges20 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 2

The correct syntax would be window.scrollTo(xpos,ypos). This works in all major browsers, read about the function here.

why don't you try to scrollIntoView the element like this

JavascriptExecutor jse=(JavascriptExecutor)driver;
jse.executeScript("document.getElementById(<id>).scrollIntoView(true)");

Faced similar issue. This works for me as an alternative for window.scrollTo JS method in all browsers.

I had the same issue, but with IE 9.

I found that

executeScript("window.scrollTo(0,100)")

did not work, but

executeScript("window.scrollTo(0,100);")

did work. Note the ";" at the end of the executed mand.

Apparently there are issues with JavascriptExecutor in certain versions of IE10. Here provides a possible workaround but I have not tried it myself.

I am using below method for scrolling in case of Internet explorer (where element is the reference of WebElement, which needs to be scrolled to center of screen):

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,-300)", "");

For other browsers, I am using:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: \"center\"});", element);

Both of these will scroll the element to center of screen.

Do not forget to replace the desired locator instead of element.

发布评论

评论列表(0)

  1. 暂无评论