Reputation: 121
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?
Upvotes: 1
Views: 2200
Reputation: 2473
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)
Upvotes: 2
Reputation: 8328
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)
Upvotes: 3
Reputation: 7550
You need to use text()
IPs = driver.find_elements_by_xpath("//span[contains(text(), '10.')]/text()")
Upvotes: 2