Selenium webdriver can't click on a link outside the page

前端 未结 10 1259
走了就别回头了
走了就别回头了 2020-11-28 10:50

I am having an issue with Selenium WebDriver. I try to click on a link that is outside the window page (you\'d need to scroll up to see it). My current code is fairly standa

10条回答
  •  失恋的感觉
    2020-11-28 11:14

    It is actually possible to scroll automatically to element. Although this is not a good solution in this case (there must be a way to get it working without scrolling) I will post it as a workaround. I hope someone will come up with better idea...

    public void scrollAndClick(By by)
    {
       WebElement element = driver.findElement(by);
       int elementPosition = element.getLocation().getY();
       String js = String.format("window.scroll(0, %s)", elementPosition);
       ((JavascriptExecutor)driver).executeScript(js);
       element.click();
    }
    

提交回复
热议问题