首页 > 解决方案 > Java:找不到状态按钮

问题描述

我正在处理 Selenium 中的功能文件,但在尝试读取状态按钮时遇到问题。如果状态符号设置为“已邀请”,则应该切换到不同的管理员帐户应用程序并批准邀请。但是,在这种情况下它没有这样做。有什么建议吗?

 public void disconnectFromUser(String user) {
    logger.info("Making sure that I am not connected to PurpleHS user");
    iframeExit();
    searchForUser(user);
    communityFrame();
    try {
        String buttonText = driver.findElement(By.className("cp-ur-link-wrapper ")).findElement(By.tagName("a")).getText();
        if (buttonText.equals("Invited")) {
            acceptConnectionRequestByHSUser();
            searchForUser(user);
            communityFrame();
            clickDisconnectBtn();
        } else {
            clickDisconnectBtn();
        }
    } catch (NoSuchElementException ex) {
        logger.info("The user is not a connection.");
        driver.findElement(By.className("close")).click();
        waitUntilPageFinishLoading();
    }
}

这是一些 HTML:

<div id="cp-ur-17417" class="cp-ur-link-wrapper "><a href="#" title="Invited" class="ur-pending ur-request-link ur-disabled-link">Invited</a></div>

标签: javahtmlseleniumqa

解决方案


对于这行代码:

String buttonText = driver.findElement(By.className("cp-ur-link-wrapper ")).findElement(By.tagName("a")).getText();  

您可以简单地将其替换为linkText

String buttonText = driver.findElement(By.linkText("Invited")).getText();  

在代码中:

if (buttonText.equals("Invited")) {
            System.out.println("Inside if else ladder")
            acceptConnectionRequestByHSUser();
            searchForUser(user);
            communityFrame();
            clickDisconnectBtn();
        } else {
            clickDisconnectBtn();
        }

推荐阅读