当我尝试执行如下所示的代码时,出现错误:
类型错误:"str"对象不可调用
email2_elem = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/input[1]").text()
此错误消息...
TypeError: 'str' object is not callable
。意味着您的程序调用了一个实际上是property
的function()
。
根据selenium.webdriver.remote.webelement text
是一个property
。
因此,不能将text()
作为函数调用。因此,您会看到错误。
溶液
您可以使用以下任一解决方案:
使用
text
属性:email2_elem = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/input[1]").text
使用
get_attribute("innerHTML")
方法:email2_elem = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/input[1]").get_attribute("innerHTML")
text
是一个属性,而不是一个函数。无需()
即可使用它
element.text
作为旁注,绝对xpath
"/html/body/..."
是一种糟糕的方法,它使定位器变得脆弱。您应该尝试通过唯一属性(id
、name
、class
等(或至少相对xpath
来定位元素。
试试这个
find_element(By.XPATH, "class name")
请参阅此文档链接
https://selenium-python.readthedocs.io/locating-elements.html#locating-elements