我需要点击“新”按钮。元素按钮在DOM上可见,但它不可点击,因为它是重叠的,我需要向下滚动页面的左侧以使其可点击。我正在尝试注入一些javascript但在我的情况下没有帮助:
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("scroll(" + driver.findElement(By.xpath(//div[@class = 'save-new'])).getLocation().getX() + "," + driver.findElement(By.xpath(//div[@class = 'save-new'])).getLocation().getY() + ")");
答案 0 :(得分:1)
尝试:
targetElement = driver.findElement(By.xpath("your xpath"));
JavascriptExecutor js = ((JavascriptExecutor) driver);
// This:
js.executeScript("arguments[0].scrollIntoView(true);", targetElement);
targetElement.click();
// Or maybe even just:
js.executeScript("arguments[0].click();", targetElement);
答案 1 :(得分:1)
我觉得@damian应该可以工作,但是你也可以尝试我的代码我用了很多次
使用此代码:
WebElement element = driver.findElement(By.xpath("Value"));
((JavascriptExecutor)driver).executeScript(“arguments[0].scrollIntoView();”, element);
element.click();
答案 2 :(得分:1)
您可以尝试这种方式: -
JavascriptExecutor js = ((JavascriptExecutor) driver);
//Scroll your page to down using below code
((JavascriptExecutor)driver).executeScript(“window.scroll(100,2000)”);
// click on button
driver.findElement(By.xpath(//div[@class ='save-new'])).click()
希望这可以帮助你:)