Selenium Chrome WebDriver how to scroll horizontally

耗尽温柔 提交于 2019-12-11 01:55:06

问题


Chrome web driver has a limitation that only loads webpage content that is in view. I have a website that has long horizontal table. I have Xpath that extracts column headers of a table that stretches more than the screen width. In chrome dev tool console if I run the xpath $x(myxpathgoeshere) I get all the headers including the ones that are not in view(the one that makes you scroll to see all). So I know my xpath is correct. But in code, when I access it by using selenium webdriver it only gives header names that are in current view. I came across various posts on chrome webdriver google group page, users mentioning this limitation and answer to it was to not fix it. So anyways, now I am trying to make it work using javascript to scroll horizontally and then do the findelement by xpath again to see if the elements to the right are loaded. But for some strange reason I cannot seem to get the scrolling horizontally to work. I am using C# Javascript executor.

IJavaScriptExecutor js = (IJavaScriptExecutor) Driver;
js.ExecuteScript("scrollTo(3000,0);"); // whatever X value I use chrome is not scrolling to the right. 

I have also tried scrollX and no luck. Is there something wrong with my code?

Edited: forgot that I was using X for horizontal not Y


回答1:


for this type of issue i had use browser zoom-in and zoom-out functionality but i am using this in java. With java robot class i am doing browser zoom-out so i automatically shows the hidden column. you can try that it may help you.




回答2:


Change the code:

js.ExecuteScript("scrollTo(3000,0);");

to

js.ExecuteScript("scroll(3000,0);");

Simply, it's not scrollTo and scroll only.



来源:https://stackoverflow.com/questions/26104952/selenium-chrome-webdriver-how-to-scroll-horizontally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!