1

I am attempting to extract IP addresses from a webpage using the following code (snippet).

IPs = driver.find_elements_by_xpath("//span[contains(text(), '10.')]")
print(IPs)

The HTML element(s) look like this:

<span style="padding: 1px 4px;float:left;">10.20.20.20</span>

Inspect >> Copy Xpath returns:

//*[@id="T1700010887"]/tbody/tr[2]/td[1]/nobr/span

But all my code prints is what looks like generic selenium code:

[<selenium.webdriver.remote.webelement.WebElement (session="7885f3a61de714f2cb33
b23d03112ff2", element="0.5496921740104628-2")>, <selenium.webdriver.remote.webe
lement.WebElement (session="7885f3a61de714f2cb33b23d03112ff2", element="0.549692
1740104628-3")>, <selenium.webdriver.remote.webelement.WebElement (session="7885
f3a61de714f2cb33b23d03112ff2", element="0.5496921740104628-4")>]

How can I get it to print the actual IP of 10.20.20.20?

3 Answers 3

3

find_elements_by_xpath returns a list of selenium objects. You have to access to each object's text attribute. This code should do what you want:

IPs = driver.find_elements_by_xpath("//span[contains(text(), '10.')]")
IPS = [elem.text for elem in IPs]
print(IPs)
Sign up to request clarification or add additional context in comments.

Comments

2

When using Selenium's element finding methods, you're retrieving a WebElement object. What you want is the element's text, which you can retrieve via the text attribute of your WebElement object. Also, the find_elements_by_xpath method returns a list of WebElements, so you need to iterate it:

IPs = driver.find_elements_by_xpath("//span[contains(text(), '10.')]")

for ip in IPs:
    print(ip.text)

1 Comment

Excellent explanation and works perfectly. Thank you!
2

You need to use text()

IPs = driver.find_elements_by_xpath("//span[contains(text(), '10.')]/text()")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.