5

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>
        submitter@asd.com
        <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\nsubmitter@asd.com\nasdas and then extract the desired text.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jose
  • 336
  • 2
  • 4
  • 16

5 Answers5

3

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: submitter@asd.com
// 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: submitter@asd.com
Account: asdas
I am text after  Account:  asdas
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • MyCompleteText gives: submitter submitter@asd.com asdas and does not give inner text insides span nodes! – Jose Apr 11 '16 at 09:22
  • sorry i am not able to get you are saying your are not getting any Name: E-mail address: Account : if yes then plz look i have also posted output and clearly shows all stuffs (inner text insides span ) indside the span nodes – Rajnish Kumar Apr 11 '16 at 09:59
1

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

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(., 'submitter@asd.com')][contains(., 'asdas')]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KPP
  • 9
  • 2
-2

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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paras
  • 3,197
  • 2
  • 20
  • 30
  • Sorry but it is wrong, the text dose not belong to element. I think I explained it clearly in the question and it is visible in the HTML sample. Please let me know if it is vague, then I will modify my questions. Thank you for your answer. – Jose Apr 11 '16 at 07:48
  • I've modified my xpath – Paras Apr 11 '16 at 07:51