【发布时间】:2017-10-04 04:26:35
【问题描述】:
为什么我的 WebDriver 等待方法没有始终点击按钮?
例如,在每 100 次测试中,有 7 次会失败,说定位器不可见,但是当查看屏幕截图时,按钮显然在那里!
<input class="btn btn-default dropdown-toggle buynow" aria-expanded="false" value="Buy Now" type="submit"/>
我尝试过等待、正常点击、循环和 JS 点击等 nothink 始终点击按钮。
public void waitAndClickElement(WebElement element) throws InterruptedException {
boolean clicked = false;
int attempts = 0;
while (!clicked && attempts < 1000) {
try {
wait.until(ExpectedConditions.elementToBeClickable(element)).click();
System.out.println("Successfully clicked on the WebElement: " + element.toString());
clicked = true;
} catch (Exception e) {
System.out.println("Unable to wait and click on WebElement" + element + ", Exception: " + e.getMessage());
Assert.fail("Method failed: waitAndClickElement");
//Assert.fail("Unable to wait and click on the WebElement, using locator: " + element.toString());
}
attempts++;
}
}
public void clickOnBuyNowButton() throws InterruptedException {
WebElement buyNowButton = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[1]/div/div/section/div[2]/div[2]/div[2]/form/div/input"));
WaitUntilWebElementIsVisible(buyNowButton);
Actions action = new Actions(driver);
action.moveToElement(buyNowButton).doubleClick().build().perform();
}
有什么想法吗?我做错了什么吗?
感谢您的帮助
【问题讨论】:
标签: java selenium selenium-webdriver webdriver