java 如何使用 selenium webdriver 进行水平滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42507826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:42:03  来源:igfitidea点击:

How to do horizontal scroll using selenium webdriver

javaseleniumselenium-webdriverscrollbar

提问by Jainish Kapadia

I want to perform horizontal scroll using on this site

我想在这个网站上使用水平滚动

1) First reduce the browser size until you will see table scroll bar.

1) 首先减小浏览器大小,直到您将看到表格滚动条。

I have done with following code snippet, but it didn't work for me well.

我已经完成了以下代码片段,但对我来说效果不佳。

driver.get("https://weather.com/en-IN/weather/5day/l/USID0011:1:US");
Dimension d = new Dimension(521,628);
driver.manage().window().setSize(d);
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

My code is working well till now, but my execution is getting stopped from here.

到目前为止,我的代码运行良好,但我的执行从这里停止。

I have try this scrolling issue with Action classbut, same issue is getting replicated.

我已经尝试过这个滚动问题,Action class但是,同样的问题正在被复制。

Actions act = new Actions(driver);
WebElement horizontal_scroll = driver.findElement(By.xpath("//div[@id='twc-scrollabe']/table"));
act.clickAndHold(horizontal_scroll).moveByOffset(40, 0).release().perform();

I have try with this below method also, but same issue is getting replicated.

我也尝试过下面的方法,但同样的问题正在被复制。

WebElement horizontal_scroll = driver.findElement(By.xpath("//div[@id='twc-scrollabe']/table"));
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript(driver.execute_script("arguments[0].scrollIntoView()", horizontal_scroll));

In my application, I can not go with this method because there no styleattribute is available inside tabletag.

在我的应用程序中,我不能使用这种方法,因为标签style内部没有可用的属性table

JavascriptExecutor jse = (JavascriptExecutor) driver;     
jse.executeScript("document.getElementById(''twc-scrollabe').setAttribute('style', 'position: relative; top: 0px; left: -658px; width: 1461px;')");

Can anyone help me on this issue?
Your help must be appreciated.
Best Regards,
Jainish Kapadia

任何人都可以帮助我解决这个问题吗?
必须感谢您的帮助。
最好的问候,
Jainish Kapadia

回答by Andersson

Simply use below for horizontal scroll

只需使用下面的水平滚动

JavascriptExecutor jse = (JavascriptExecutor) driver;     
jse.executeScript("document.querySelector('table th:last-child').scrollIntoView();");

This should allow to scroll to the last column of the table

这应该允许滚动到表格的最后一列