9

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?

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The XPath locator provided is logical and works in Chrome's console, but it won't work in Selenium. Selenium will throw the following error... The given selector //div[@class='StdLevel1']/text() is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: The result of the xpath expression "//div[@class='StdLevel1']/text()" is: [object Text]. It should be an element.
So is there a solution or would you just grab the div and parse the getText() string?
To my understanding, Infact selenium does to allow text(), node etc.function.
4

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.