Search code examples
pythonseleniumselenium-webdrivertextinnertext

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)

So to sum this thing up, I made a "search bot" in selenium that opens google chrome and searches with ecosia. I then grabbed the links and sent them back into the results array. However I get this error, and it only displays on the title part

  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 then tried to only use links[i] but when I try to return it back it says that I cannot concatenate a string (href) with a web element (title)

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

Solution

  • text seems to not be a callable so just use

    title = links[i].text