What's the get_Text() equivalent in python bindings for Selenium/Webdriver

青春壹個敷衍的年華 提交于 2020-12-05 11:56:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!