In Selenium Webdriver, how to get a text after an element?

11,220

Solution 1

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: [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 output will be like this

My Complete Text is : Name: submitter
E-mail address: [email protected]
Account: asdas
I am text after  Account:  asdas

Hope this helps you

Solution 2

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);
Share:
11,220
Jose
Author by

Jose

Updated on June 27, 2022

Comments

  • Jose
    Jose almost 2 years

    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>
            [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:

    //*[@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\[email protected]\nasdas and then extract the desired text.

  • Jose
    Jose about 8 years
    Sorry but it is wrong, the text dose not belong to <span> 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.
  • Paras
    Paras about 8 years
    I've modified my xpath
  • Jose
    Jose about 8 years
    MyCompleteText gives: submitter [email protected] asdas and does not give inner text insides span nodes!
  • eduliant
    eduliant about 8 years
    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