简体   繁体   中英

Selenium Chrome WebDriver how to scroll horizontally

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

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.

Change the code:

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

to

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

Simply, it's not scrollTo and scroll only.

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