繁体   English   中英

TypeError: 'str' object is not callable error using text() from Selenium Python

[英]TypeError: 'str' object is not callable error using text() from Selenium Python

links = browser.find_elements_by_xpath("//h2[@class='result-firstline-title highlighted-domain-title']//a")
    results = []
    for i in range(len(links)):
        title = links[i].text()
        href = links[i].get_attribute("href")
        results.append(title)
        results.append(href)

所以总结一下,我在 selenium 中创建了一个“搜索机器人”,它可以打开 google chrome 并使用 ecosia 进行搜索。 然后我抓取链接并将它们发送回结果数组。 但是我收到了这个错误,它只显示在标题部分

  File "c:/Users/icisr/OneDrive/Desktop/Bot/Searchbot.py", line 45, in get_results
    title = links[i].text()
TypeError: 'str' object is not callable

然后我尝试仅使用 links[i] 但是当我尝试将其返回时它说我无法将字符串(href)与 web 元素(标题)连接起来

str1 = ""
  for ele in results:
    str1 += ("  " + ele)
  return str1

文本似乎不是可调用的,所以只需使用

title = links[i].text

文本属性

textWebElement的属性,但不是方法。

因此,您需要将text()替换为text ,您的有效代码行将是:

title = links[i].text    

只需将text()替换为仅text

title = links[i].text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM