Getting different texts from the HTML DOM through Selenium and Python

0 votes

Look the below code.:

<tr>

    <td>

    </td>


    <td>

    </td>


    <td>

    </td>


    <td>

    </td>


    <td>

        text1

        <br>

        <img>

        <br>

        text2

    </td>

</tr>

When I try to get the text in the 5th td like so:

something = elem.find_element_by_xpath('./td[5]').text

I save/get both texts in the same variable. Can I get them in an individual variables so that we can avoid splitting it? Any suggestions….????

something = elem.find_element_by_xpath('./td[5]/text()[1]')

I get the following error message:

InvalidSelectorException: invalid selector: 

The result of the xpath expression "./td[5]/text()[1]" is: [object Text]. 

It should be an element.

Above is the error:

May 14, 2018 in Selenium by Martin
• 4,320 points
2,801 views

1 answer to this question.

0 votes

Try Below code:

something = elem.find_element_by_xpath('./td[5]')

text1 = driver.execute_script('return arguments[0].firstChild.textContent;', something).strip()

text2 = driver.execute_script('return arguments[0].lastChild.textContent;', something).strip()

Ty to get two separate text nodes

answered May 14, 2018 by Samarpit
• 5,910 points

Related Questions In Selenium

0 votes
0 answers

Getting different texts from the HTML DOM through Selenium and Python or vb.net

Apologies in advance had to hide info ...READ MORE

May 4, 2020 in Selenium by Rock
• 120 points
912 views
0 votes
1 answer

How are 'Selenium-server-standalone.jar' and 'Selenium Client & WebDriver' different from each other?

Okay, your understanding of "Selenium Server" & ...READ MORE

answered Apr 6, 2018 in Selenium by nsv999
• 5,500 points
2,634 views
+2 votes
1 answer

I want the console.log output from Chrome. I'm working with selenium on Python

So this is how you do it ...READ MORE

answered May 3, 2018 in Selenium by sniffy_god
• 780 points
40,681 views
0 votes
3 answers

How to click the search button using Selenium web driver and Python

You can try with tag. #this code will ...READ MORE

answered Apr 10, 2019 in Selenium by Matin
15,676 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
12,715 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
8,608 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
6,684 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,551 views
0 votes
1 answer

Exclude text from `<script>` tag within the HTML DOM when calling `element.getText()`

As you have already identified the WebElement as element next you ...READ MORE

answered May 8, 2018 in Selenium by Samarpit
• 5,910 points
1,305 views
+1 vote
1 answer
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