类型错误:“str”对象不可通过 Python 使用 Selenium 调用

szy*_*d45 5 python selenium properties webdriver selenium-webdriver

当我尝试执行下面显示的代码时,出现错误:

类型错误:“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()
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 7

这个错误信息...

TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)

...暗示您的程序已经调用了function()实际上是 a 的 a property

根据selenium.webdriver.remote.webelement text是一个property.

因此,您不能text()作为函数调用。因此,您会看到错误。

解决方案

您可以使用以下任一解决方案:


Guy*_*Guy 5

text是一个属性,而不是一个函数。使用它无需()

element.text
Run Code Online (Sandbox Code Playgroud)

顺便说一句,绝对xpath "/html/body/..."是一种不好的方法,它会使定位器变得脆弱。id您应该尝试通过唯一属性( 、等)或至少相对属性来name定位元素。classxpath