简体   繁体   中英

Selenium: Extract Text of a div with cssSelector in Java

I am writing a JUnit test for a webpage, using Selenium, and I am trying to verify that the expected text exists within a page. The code of the webpage I am testing looks like this:

<div id="recipient_div_3" class="label_spacer">
   <label class="nodisplay" for="Recipient_nickname"> recipient field: reqd info </label>
   <span id="Recipient_nickname_div_2" class="required-field"> *</span>
   Recipient:
</div>

I want to compare what is expected with what is on the page, so I want to use Assert.assertTrue(). I know that to get everything from the div, I can do

String element = driver.findElement(By.cssSelector("div[id='recipient_div_3']")).getText().replaceAll("\n", " ");

but this will return "reqd info * Recipient:"

Is there any way to just get the text from the div ("Recipient") using cssSelector, without the other tags?

You can't do this with a CSS selector, because CSS selectors don't have a fine-grained enough approach to express "the text node contained in the DIV but not its other contents". You can do that with an XPath locator, though:

driver.findElement(By.xpath("//div[@id='recipient_div_3']/text()")).getText()

That XPath expression will identify just the single text node that is a direct child of the DIV, rather than all the text contained within it and its child nodes.

I am not sure if it is possible with one css locator, but you can get text from div, then get text from div's child nodes and subtract them. Something like that (code wasn't checked):

String temp = "";
List<WebElement> tempElements = driver.findElements(By.cssSelector("div[id='recipient_div_3'] *"));
for (WebElement tempElement : tempElements) {
    temp =+ " " + tempElement.getText();
}
String element = driver.findElement(By.cssSelector("div[id='recipient_div_3']")).getText().replaceAll("\n", " ").replace(temp, "");

This is for case when you try to avoid using xpath. Xpath allows to do it:

//div[@id='recipient_div_3']/text()

You could also get the text content of an element and remove the tags with regexp. Also notice: you should use the reluctant quntifier https://docs.oracle.com/javase/tutorial/essential/regex/quant.html

String getTextContentWithoutTags(WebElement element) {
    return element.getText().replaceAll("<[^>]*?/>", "").trim();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM