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,220 points
427 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,130 points

Related Questions In Selenium

0 votes
1 answer
+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
10,018 views
0 votes
3 answers
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 in Selenium by Abha
• 22,960 points
79 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,090 points
566 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
895 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
559 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,130 points
204 views
+1 vote
1 answer