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,340 points
1,250 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,890 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 in Selenium by Rock
• 120 points
167 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,510 points
1,589 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
23,951 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
9,867 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,510 points
3,547 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,552 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,890 points
2,603 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 2 in Selenium by Sri
• 2,970 points
2,878 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,890 points
346 views
+1 vote
1 answer