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
831 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,870 points

Related Questions In Selenium

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,510 points
1,287 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
17,829 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
6,367 views
0 votes
1 answer

What are locators in Selenium and what are the different types of it?

Hi Divya, in Selenium Locators define an ...READ MORE

answered May 7, 2019 in Selenium by Abha
• 27,800 points
261 views
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,510 points
2,194 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,870 points
1,815 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,870 points
1,796 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,870 points
287 views
+1 vote
1 answer