Reputation: 336
I would like to get a text that exactly comes after a specific element. Please look at the sample code:
<div id="content-tab-submitter" class="">
<h4>Sender</h4>
<p>
<span class="screenHidden">Name: </span>
submitter
<br>
<span class="screenHidden">E-mail address:</span>
[email protected]
<br>
<span class="screenHidden">Account: </span>
asdas
<br>
</p>
</div>
I would like to get the text that comes exactly after <span>
which contains "Account".
By using this XPath expression:
//*[@id='content-tab-submitter']/p/span[contains(text(),'Account')]/following::text()
Java gives me an error, since the output is not a web element and it is a text. So, it is not possible to use findElement.
How can I get this text in a clean way? I mean I do not want to get all texts (in this case): submitter\[email protected]\nasdas and then extract the desired text.
Upvotes: 5
Views: 13623
Reputation: 9
I hope this works perfectly fine. If not, then please add a div class in front of the span class.
//span[@class='screenHidden'][contains(., 'submitter')][contains(., '[email protected]')][contains(., 'asdas')]
Upvotes: -1
Reputation: 2938
I think there isn't any straight possible way to extract text just after Account: i.e., an XPath expression which refers to text = 'asdas' directly.
So instead of trying to read exactly text = 'asdas' directly, try to read every text in the p tag and perform an operation like below to extract the tag.
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/rajnish/Desktop/myText.html");
// XPath expression for talking complete text i.e all text inside every span tag that comes under p tag
String MyCompleteText = driver.findElement(By.xpath("//*[@id='content-tab-submitter']/p")).getText();
System.out.println("My Complete Text is: " + MyCompleteText);
// MyCompleteText outputs in console:
// My Complete Text is: Name: submitter
// E-mail address: [email protected]
// Account: asdas
// Now we have to split our MyCompleteText on the basis of Account:
// as we want text after Account: so do it like below
String[] mytext = MyCompleteText.split("Account:");
System.out.println("I am text after Account: "+ mytext[1]);
And the output will be like this:
My Complete Text is: Name: submitter
E-mail address: [email protected]
Account: asdas
I am text after Account: asdas
Upvotes: 3
Reputation: 3235
You can use the getText() method of Selenium, something like:
driver.findElement(By.xpath("XPath expression of the element")).getText();
Your XPath expression can be:
//span[contains(.,'Account')]/following::text()
Upvotes: -2
Reputation: 193198
To get the text, e.g., asdas, that comes exactly after <span>
with innerText as Account, you can use the following lines of code:
// Identify the parent node
WebElement myElement = driver.findElement(By.xpath("//div[@id='content-tab-submitter']//p"));
String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[8].textContent;', myElement).strip()
// or
String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[10].textContent;', myElement).strip()
Upvotes: 0
Reputation: 42528
You can use a piece of JavaScript code to get the following text node:
// Get the targeted element
WebElement element = driver.findElement(By.cssSelector("id('content-tab-submitter')/p/span[contains(.,'Account')]"));
// Get the following text
String text = (String)((JavascriptExecutor)driver).executeScript(
"return arguments[0].nextSibling.textContent.trim()", element);
Upvotes: 1