【问题标题】:Why are my WebDriver wait methods not consitently clicking on the button?为什么我的 WebDriverwait 方法不经常点击按钮?
【发布时间】: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


    【解决方案1】:

    尝试关注;在这里,我首先等待一个元素是可点击的,然后一旦它变成可点击的,我就会移动到它,然后点击:

    WebDriverWait wait = new WebDriverWait (driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(yourElement));
    
    Actions act = new Actions(driver);
    act.moveToEelment(yourElement).click().build().perform();
    

    【讨论】:

    • @kushal.even that dosnt work :/ 它就好像即使设置了 15 秒的超时,它也不会继续尝试单击该元素
    • 20 秒过后会发生什么?
    • @kushal.an 会抛出异常,例如无法单击元素或定位器不可见,但我已捕获屏幕截图并且按钮在那里
    • Selenium 仅与 DOM 一起工作,因此如果元素应该存在于其中以便 selenium 进行交互;无论如何,您尝试过JavascriptExecutor吗?和动作类?
    • 是的,恐怕我有:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多