Prabu
Prabu

Reputation: 3728

Webdriver - getting div value using java

Html

<div class="rc-anchor-light">
<div id="accessible-status" class="rc-anchor-aria-status" aria-hidden="true">requires verification.</div>
</div>

My expected

Extract the text from inside the div "requires verification."

tried below locator, but i'm not getting my expected.

unique id so i tired with id getting empty result

String accessText = driver.findElement(By.id("accessible-status")).getText();

using class name getting empty result

String accessText = driver.findElement(By.className("rc-anchor-aria-status")).getText();

Please suggest me how to get the value.

Upvotes: 0

Views: 283

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

As per the HTML you have shared to extract the text requires verification. from the div you need to use a Locator Strategy to identify the WebElement uniquely and induce WebDriverWait with ExpectedConditions set as visibilityOfElementLocated(By locator) for the WebElement to be visible in the HTML DOM and finally use getAttribute() method to to extract the text. You can use the following line of code :

System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='rc-anchor-light']/div[@class='rc-anchor-aria-status' and @id='accessible-status']"))).getAttribute("innerHTML"));

Upvotes: 1

Andersson
Andersson

Reputation: 52665

Text content of target div node might be generated dynamically. Try to implement ExplicitWait for element with any text:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement divNode = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='accessible-status' and normalize-space()]"));
String accessText = divNode.getText();

If element has text from the very beggining, but it is just hidden, getText() will not return you text. In this case you may try

String accessText = driver.findElement(By.id("accessible-status")).getAttribute("textContent");

Upvotes: 2

Related Questions