【问题标题】:TypeError: 'str' object is not callable error using text() from Selenium PythonTypeError: 'str' object is not callable error using text() from Selenium Python
【发布时间】:2021-04-21 12:01:12
【问题描述】:
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

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

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

【问题讨论】:

    标签: python selenium selenium-webdriver text innertext


    【解决方案1】:

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

    title = links[i].text
    

    【讨论】:

      【解决方案2】:

      文本属性

      textWebElement 的属性,但不是方法。

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

      title = links[i].text    
      

      【讨论】:

        【解决方案3】:

        只需将text() 替换为text

        title = links[i].text
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-09
          • 2018-09-15
          • 1970-01-01
          • 2020-03-27
          • 2023-03-18
          • 2016-01-01
          相关资源
          最近更新 更多