Michal
Michal

Reputation: 3276

Selenium (java) can't get text from nested element without printing it

I try to get text from title of modal pop-up window. On the page there are many such windows - each with uniqe id. In each such modal window all elements have the same class names so first I need to point to the correct window and then look for particular element. So I do it with this code:

public String getRFRTitle(String rfrNumber) {
    return driver.findElement(By.id("rfr-details-dialog-"+rfrNumber)).
            findElement(By.className("modal-title")).getText();  
}

But it's not displaying for me anything. What I have found is that when I print this title text before, this function works correctly. I added this before returning value from the function:

System.out.println("tite: "+ driver.findElement(By.id("rfr-details-dialog-"+rfrNumber)).
        findElement(By.className("modal-title")).getText());

I tried with initialisation of variables before returning text but without luck. I can go with my workaround but I'm curious is this Java or Selenium issue.

Upvotes: 0

Views: 1418

Answers (1)

joostschouten
joostschouten

Reputation: 3893

This will most likely be a timeout issue. The following might work for you and if it doesn't, the stacktrace will give you more feedback.

By locator = By.cssSelector("#rfr-details-dialog-" + rfrNumber + " .modal-title");
int timeoutInSeconds = 10;

WebElement foundElement = new WebDriverWait(webdriver, timeoutInSeconds).until(ExpectedConditions.visibilityOfElementLocated(locator));

System.out.println("tite: " + foundElement.getText());

Upvotes: 1

Related Questions