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

Viewed 246
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
3 Answers

text seems to not be a callable so just use

title = links[i].text

text attribute

text is an attribute of a WebElement but not a method.

So you need to replace text() with text and your effective line of code will be:

title = links[i].text    

just replace text() with only text

title = links[i].text
Related