java - Webdriver 尝试单击按钮但收到 'server did not provide a static trace' ?

标签 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), 

最佳答案

听起来像是计时问题,使用显式等待来定位按钮

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()

关于java - Webdriver 尝试单击按钮但收到 'server did not provide a static trace' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44020126/

相关文章:

java - 使用 Eclipse 的 ClassNotFoundException/NoClassDefFoundError

python - Selenium select_by_value 然后打印选项文本

java - Selenium 2.53 不适用于 Firefox 47

selenium - 我们如何循环不同的基本URL而不是默认基本URL并运行功能测试?

java - selenium webdriver 在左侧移动 slider

java - 尝试在java中运行从cpp文件创建的exe文件

Java 到 XML 的转换?

java - 按钮不显示文本区域中的文本

google-chrome - Selenium 中 ChromeDriver 的随机 NoHttpResponseException

javascript - 如何在 NodeJS Selenium 中使用代理?