如何使用Selenium WebDriver进行水平滚动 [英] How to do horizontal scroll using selenium webdriver

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

问题描述

我想在此网站

1)首先减小浏览器的大小,直到看到表格滚动条.

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

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

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.

我已经尝试使用Action class来解决此滚动问题,但是同样的问题正在被复制.

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

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));

在我的应用程序中,我不能使用此方法,因为table标记内没有可用的style属性.

In my application, I can not go with this method because there no style attribute is available inside table tag.

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

有人可以帮助我解决这个问题吗?
感谢您的帮助.
最好的问候,
贾尼什·卡帕迪亚(Jainish Kapadia)

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

推荐答案

在下面仅使用水平滚动即可

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

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

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