4

I would like to get a text that exactly comes after a specific element. Pleases look 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:

//*[@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.

Is there any idea how to 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.

ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
Jose
  • 316
  • 2
  • 4
  • 15
  • 1
    Unfortunately, you can't do that using just `findElement()`. Related question : [Getting text from a node](http://stackoverflow.com/questions/8505375/getting-text-from-a-node/) – har07 Apr 11 '16 at 07:51
  • @har07 Thank you very much. Unfortunately, there is no other way! – Jose Apr 11 '16 at 07:53
  • yes there is a way plz check my answer below – eduliant Apr 11 '16 at 07:55

5 Answers5

3

HI i think there is no straight possible way to extract text just after Account: i.e a xpath 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 operation like below to extract the tag.

WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/rajnish/Desktop/myText.html");

// xpath 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 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

Hope this helps you

eduliant
  • 2,690
  • 4
  • 22
  • 36
  • 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 – eduliant Apr 11 '16 at 09:59
1

You can use a piece of Javascript 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);
Florent B.
  • 39,675
  • 6
  • 77
  • 97
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()
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
-1

Hope this works perfectly fine. If not then please add div class in front of span class.

//span[@class='screenHidden'][contains(., 'submitter')][contains(., 'submitter@asd.com')][contains(., 'asdas')]
Petter Friberg
  • 20,644
  • 9
  • 57
  • 104
KPP
  • 9
  • 2
-2

You can use getText() method of Selenium, something like:-

driver.findElement(By.xpath("xpath of the element")).getText();

your xpath can be:-

//span[contains(.,'Account')]/following::text()
Paras
  • 3,076
  • 2
  • 19
  • 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