简体   繁体   中英

How to get text from html tag span through Selenium and Python

Here is my HTML and trying to get the text must be unique. with pyhton 2.7

我的图片html

Tried to use:

driver.find_element_by_xpath("//span[@id='lblError']/text()")

The xpath was not valid one.

try to find the element by ID then get_attribute("innerHTML").

var el = driver.find_element_by_id("lblerror")
var text = el.get_attribute("innerHTML")

To retrieve the text must be unique you can use the execute_script() method and you can use the following solution:

myElement = driver.find_element_by_xpath("//span[@id='lblError']")
myText = driver.execute_script('return arguments[0].lastChild.textContent;', myElement).strip()

enter code here enter code here time.sleep(1) enter code here element = driver.find_element_by_xpath("//span[@id='lblError']") enter code here text = element.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