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,100 points
7,683 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,460 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,300 points
9,313 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,360 points
7,874 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, 2019 in Selenium by Anvi
• 14,150 points
6,785 views
0 votes
2 answers

Is there any way to get the text of a web element using Selenium Webdriver?

use gettext() in java : string lableText = ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
8,485 views
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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,460 points
32,553 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,460 points
16,459 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP