问题
I would like to move from Selenium 1 to Selenium 2. I use python binding however I can not find any get_text()
functions.
eg. selenium.find_elements_by_css_selector("locator").get_text()
Is there such function in python bindings for Selenium/Webdriver ?
回答1:
use the '.text' property.
element.text
回答2:
This line of code...
selenium.find_elements_by_css_selector("locator").get_text()
...have exactly two (2) issues to be addressed.
Details
As per the current documentation get_Text()
can be implemented in two different approaches:
Using the text attribute: Gets the text of the element.
Using the get_attribute("innerHTML"): Gets the innerHTML of the element.
Additionally, text attribute or get_attribute("innerHTML")
can be applied on a WebElement but not on WebElements. Hence find_elements*
needs to be replaced with find_element*
Solution
Effectively, your line of code will be:
Using text attribute:
selenium.find_element_by_css_selector("locator").text
Using
get_attribute("innerHTML")
:selenium.find_element_by_css_selector("locator").get_attribute("innerHTML")
来源:https://stackoverflow.com/questions/6360939/whats-the-get-text-equivalent-in-python-bindings-for-selenium-webdriver