Rupali Waghaye
Rupali Waghaye

Reputation: 13

Selenium WebDriver Java - I'm not able to get dynamic text from span

static By yearFoundErrorMessage = By.className("error");    
public static WebElement ca1_YearFoundError(WebDriver driver){
        element = driver.findElement(By.id("yearFoundErrorMessage"));
        return element;
    }

//Main class from where I'm calling above function
String errorMessage = Ca1GeneralInformation.ca1_YearFoundError(driver).getText();
        System.out.println(errorMessage);


<div class="controls">
<input id="ca1_GEN_foundedDate" class="error" type="text" value="2008" maxlength="4" name="data[CaInformation][ca1_GEN_foundedDate]" tabindex="5" unsaved="true">
<span id="ca1_GEN_foundedDate_error" class="inline-error ajax-failed-msg">
<span class="error" for="ca1_GEN_foundedDate">Please enter a valid year (i.e. 2013)</span>
</span>
</div>

I'm working on webapplication where there is error message in span I want to get text of this span using webdriver. Above is my html and code.Please help me out. thanks in advance.

Upvotes: 0

Views: 496

Answers (2)

Mosam Mehta
Mosam Mehta

Reputation: 1668

Try below xPath :

WebElement element =  driver.findElement(By.xpath("//span[contains(text(),'Please enter a valid year')]");
String message = element.getText();

Upvotes: 0

Guy
Guy

Reputation: 50809

yearFoundErrorMessage is a By variable but you are sending it as string to another By. Try this

public static WebElement ca1_YearFoundError(WebDriver driver) {
    element = driver.findElement(yearFoundErrorMessage);
    return element;
}

Upvotes: 1

Related Questions