简体   繁体   中英

Extract text from webpage with Selenium

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?

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)

您需要使用text()

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM