使用Selenium WebDriver和Java滚动

Rog*_*y L 11 java selenium webdriver selenium-webdriver

我正在使用Selenium WebDriver自动化我的浏览器测试. 我的浏览器标题是浮动的,无论浏览器滚动如何,它总是存在.

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

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

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

小智 5

    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+");");
Run Code Online (Sandbox Code Playgroud)