Selenium Chrome WebDriver如何水平滚动 [英] Selenium Chrome WebDriver how to scroll horizontally

查看:230
本文介绍了Selenium Chrome WebDriver如何水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Chrome Web驱动程序有一个限制,即仅加载可见的网页内容.我有一个水平表较长的网站.我有Xpath提取表的列标题,该表的标题超出屏幕宽度.在chrome开发工具控制台中,如果我运行xpath $ x(myxpathgoeshere),则会得到所有标头,包括不在视图中的标头(使您滚动以查看所有标头的标头).所以我知道我的xpath是正确的.但是在代码中,当我使用Selenium Webdriver访问它时,它仅提供当前视图中的标头名称.我在chrome webdriver google群组页面上看到了各种帖子,用户提到了此限制,并且对此的回答是不解决它.因此,无论如何,现在我正在尝试使用javascript使其水平滚动,然后再次通过xpath执行findelement,以查看右侧的元素是否已加载.但是出于某些奇怪的原因,我似乎无法使水平滚动正常工作.我正在使用C#Javascript执行程序.

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. 

我也尝试过scrollX,但没有运气.我的代码有问题吗?

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

忘记了我在水平方向使用X而不是Y

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

推荐答案

更改代码:

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

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

简而言之,它不仅是scrollToscroll.

Simply, it's not scrollTo and scroll only.

这篇关于Selenium Chrome WebDriver如何水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆