Selenium Chrome WebDriver how to scroll horizontal

2019-07-20 02:27发布

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

2条回答
地球回转人心会变
2楼-- · 2019-07-20 03:01

Change the code:

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

to

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

Simply, it's not scrollTo and scroll only.

查看更多
我命由我不由天
3楼-- · 2019-07-20 03:09

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.

查看更多
登录 后发表回答