Reading text using selenium webdriver XPath

0 votes

I'm using selenium to get some text on my webpage using XPath.

<span id="data" class="firefinder-match">
    Seat Height, Laden
  <sup>
     <a class="speckeyfootnote" rel="p7" href="#">7</a>
  </sup>
</span>

If I use the following code -

driver.findElement(By.xpath("//span[@id='data']")).getText();

I get the result = Seat Height, Laden 7

But I want to avoid reading the text within the <sup> tags and get the result Seat Height, Laden

Please let me know which XPath expression I can use to get my desired result.

Aug 9, 2018 in Selenium by Perry
• 17,020 points
2,235 views

1 answer to this question.

0 votes

I don't know about how to do this in Selenium, so there's my JS solution. The idea is to get all children of the element (including the text nodes) and then select only the text nodes. You might need to add some .trim() (or JS equivalent) calls to get rid of unneeded spaces.

The complete code:

WebElement elem = driver.findElement(By.id("data"));
String text;
if (driver instanceof JavascriptExecutor) {
    text = ((JavascriptExecutor)driver).executeScript(
            "var nodes = arguments[0].childNodes;" +
            "var text = '';" +
            "for (var i = 0; i < nodes.length; i++) {" +
            "    if (nodes[i].nodeType == Node.TEXT_NODE) {" +
            "        text += nodes[i].textContent;" +
            "    }" +
            "}" +
            "return text;"
            , elem);
}
answered Aug 9, 2018 by Meci Matt
• 9,420 points

Related Questions In Selenium

0 votes
1 answer

findElement(By.xpath()) not working using Selenium WebDriver

element = findElement(By.xpath("//*[@test-id='test-username']"); element = findElement(By.xpath("//input[@test-id='test-username']"); (*) - any ...READ MORE

answered Apr 18, 2018 in Selenium by code_ninja
• 6,220 points
2,130 views
0 votes
1 answer

Reading the pdf file using selenium webdriver

Use should use PDFBox and FontBox. ...READ MORE

answered Apr 25, 2018 in Selenium by Vardy
• 2,270 points
2,727 views
0 votes
1 answer

How can I clear the text in a text box using Selenium WebDriver?

Hello Akriti, you can clear the text ...READ MORE

answered May 29 in Selenium by Anvi
• 13,960 points
307 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
976 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,131 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
873 views
0 votes
1 answer

Clear text from text area using selenium WebDriver

use the below code: driver.find_element_by_id('abc').clear(); READ MORE

answered Jun 20, 2018 in Selenium by Meci Matt
• 9,420 points
8,049 views
0 votes
1 answer

How to enter text into text field using Selenium WebDriver

You can use the same code as ...READ MORE

answered Jul 6, 2018 in Selenium by Meci Matt
• 9,420 points
1,795 views