Webdriver试图点击按钮但接收'服务器没有提供静态跟踪'?

时间:2017-05-17 08:58:46

标签: java selenium selenium-webdriver webdriver testng

Webdriver试图点击按钮但接收'服务器没有提供静态跟踪'?

我想点击的按钮:

<div class="buynowbutton">
<input class="btn btn-default dropdown-toggle buynow" aria-expanded="false" value="Buy Now" type="submit"/>
</div>

我已经创建了下面列出的下面的方法,例如96个版本会通过,但是我会得到4个因为指示的异常而失败:

public void clickOnBuyNowButton() throws InterruptedException {
    WebElement buyNowButton = driver.findElement(By.xpath("(//input[@value='Buy Now' and @value='Buy Now'])[2]"));
    boolean clicked = false;
    int attempts = 0;
    while (!clicked && attempts < 5000) {
        try {
            Thread.sleep(2000);
            this.wait.until(ExpectedConditions.elementToBeClickable(buyNowButton)).isEnabled();
            actionMoveAndClick(buyNowButton);
            clicked = true;
        } catch (Exception e) {
            System.out.println("Unable click on 'BuyNow' button | clickOnBuyNowButton()");
            Assert.fail("Method failed: waitAndClickElement");
        }
        attempts++;
    }
}

栈跟踪

org.openqa.selenium.NoSuchElementException: 
no such element: Unable to locate element: {"method":"xpath","selector":"(//input[@value='Buy Now' and @value='Buy Now'])[2]"}
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), 

2 个答案:

答案 0 :(得分:1)

这听起来像计时问题,使用显式等待来找到按钮

WebElement buyNowButton = this.wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Buy Now'][position()=2]")));

boolean clicked = false;
int attempts = 0;
while (!clicked && attempts < 5000) {
    try {
        actionMoveAndClick(buyNowButton);
        clicked = true;
    } catch (Exception e) {
        System.out.println("Unable click on 'BuyNow' button | clickOnBuyNowButton()");
        Assert.fail("Method failed: waitAndClickElement");
    }
    attempts++;
}

您也不需要Thread.sleep(2000);isEnabled()。如果该按钮是可点击的,则启用该按钮,并且您无论如何都不检查返回的值。

此外,xpath无效。如果您想通过索引选择使用position()

答案 1 :(得分:0)

我记得我的代码中有这样的错误。再次复制xpath帮助了我。我对这个xpath有点疑惑,因为Guy所说的方面和xpath中的简单引号。我只知道xpath中的双引号,需要用反斜杠来表示。

如果无效,您可以使用另一个标识符“findElement”,例如“id”或“name”。