'Java: Unable to locate status button

I am working on a feature file in Selenium and I am having issues trying to read the status button. If the status symbol is set to "Invited," it is supposed to switch to a different admin account application and approve the invite. However, it is not doing that in this scenario. Any advice?

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

Here's some 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>


Solution 1:[1]

For this line of code :

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

You can simply replace it to linkText :

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

In code :

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

Solution 2:[2]

Your classname should be ur-pending ur-request-link ur-disabled-link not cp-ur-link-wrapper and no need to find by tagname again.

or you could also try xpath

//a[@title='Invited'][@class='ur-pending ur-request-link ur-disabled-link']

so your line of code would become:

String buttonText = driver.findElement(By.xpath("//a[@title='Invited'][@class='ur-pending ur-request-link ur-disabled-link']")).getText();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cruisepandey
Solution 2 Mahesh