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,210 points
198 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 760 points
5,237 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
• 5,760 points
19 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,030 points
60 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
426 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
46 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
165 views
+1 vote
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.