使用Selenium WebDriver和Java滚动 [英] Scrolling using Selenium WebDriver with Java

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

问题描述

我正在使用Selenium WebDriver自动化我的浏览器测试。 我的浏览器标题是浮动的,无论浏览器滚动,它始终存在。

I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.

因此,当我点击浏览器当前可见区域下方的某些元素时,selenium会尝试将元素滚动到视图中并单击它们。

So when I click on certain elements that are present below the current visible region of the browser, selenium tries to scroll the element into view and click them.

但是由于自动滚动,因此元素在浮动标题后面滚动,当对它们执行任何操作时,页面标题中的元素被点击。

But because of the auto scrolling as such the elements are scrolled behind the floating header and when any action is performed on them, the elements in the page header get clicked.

有没有办法限制WebDriver的默认滚动

推荐答案

    Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
    int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");

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

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