简体   繁体   English

Selenium Chrome WebDriver 如何水平滚动

[英]Selenium Chrome WebDriver how to scroll horizontally

Chrome web driver has a limitation that only loads webpage content that is in view. Chrome 网络驱动程序有一个限制,即只能加载可见的网页内容。 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.我有 Xpath 可以提取超出屏幕宽度的表格的列标题。 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).在 chrome 开发工具控制台中,如果我运行 xpath $x(myxpathgoeshere) 我会得到所有标题,包括不在视图中的标题(让你滚动查看所有内容的标题)。 So I know my xpath is correct.所以我知道我的 xpath 是正确的。 But in code, when I access it by using selenium webdriver it only gives header names that are in current view.但是在代码中,当我使用 selenium webdriver 访问它时,它只提供当前视图中的标头名称。 I came across various posts on chrome webdriver google group page, users mentioning this limitation and answer to it was to not fix it.我在 chrome webdriver google group page 上看到了各种帖子,用户提到了这个限制,答案是不修复它。 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.所以无论如何,现在我正在尝试使用 javascript 使其工作以水平滚动,然后再次通过 xpath 执行 findelement 以查看是否加载了右侧的元素。 But for some strange reason I cannot seem to get the scrolling horizontally to work.但出于某种奇怪的原因,我似乎无法让水平滚动工作。 I am using C# Javascript executor.我正在使用 C# Javascript 执行程序。

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.我也试过 scrollX 但没有运气。 Is there something wrong with my code?我的代码有问题吗?

Edited: forgot that I was using X for horizontal not Y编辑:忘记我使用 X 表示水平而不是 Y

for this type of issue i had use browser zoom-in and zoom-out functionality but i am using this in java.对于这种类型的问题,我使用了浏览器放大和缩小功能,但我在 java 中使用了它。 With java robot class i am doing browser zoom-out so i automatically shows the hidden column.使用 java 机器人类,我正在缩小浏览器,因此我会自动显示隐藏列。 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.简单地说,它不是scrollTo并且仅scroll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium WebDriver 和/或 Java 中的 Sikuli 平滑滚动 - How to scroll smoothly with Selenium WebDriver and/or Sikuli in Java 如何滚动到 div 元素的底部 Selenium Webdriver - How to scroll to bottom of div element Selenium Webdriver 如何使用 Selenium webdriver 滚动谷歌表格 - how to scroll google sheets with Selenium webdriver 如何在 Selenium (selenium-webdriver) JavaScript 中使用 Chrome 配置文件 - How to use Chrome profile in Selenium (selenium-webdriver) JavaScript 滚动页面 selenium webdriver JS - scroll page selenium webdriver JS 如何使用C#向下滚动Selenium WebDriver? - How to scroll down with Selenium WebDriver using C#? Selenium-webdriver / Javascript:如何滚动页面? - Selenium-webdriver/Javascript: How do I scroll through a page? 如何在 Chrome 中使用 Selenium-Webdriver for JavaScript 接受不安全的证书? - How to accept insecure certificates in Chrome using Selenium-Webdriver for JavaScript? selenium webdriver 与 javascript:如何允许 chrome 下载多个文件? - selenium webdriver with javascript : how to allow chrome to download multiple files? 如何使用 selenium webdriver 在 chrome 中定位开发者工具页面的元素 - How to locate the elements of developer tools page in chrome using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM