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
然后我尝试只使用链接[i],但是当我尝试 return 它返回时它说我不能将字符串 (href) 与网络元素 (title) 连接起来
str1 = ""
for ele in results:
str1 += (" " + ele)
return str1
文本似乎不是可调用的,所以只需使用
title = links[i].text
文本属性
text
is an attribute of a WebElement 但不是方法。
因此您需要将text()
替换为text
,您的有效代码行将是:
title = links[i].text
只需将 text()
替换为 text
title = links[i].text
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],但是当我尝试 return 它返回时它说我不能将字符串 (href) 与网络元素 (title) 连接起来
str1 = ""
for ele in results:
str1 += (" " + ele)
return str1
文本似乎不是可调用的,所以只需使用
title = links[i].text
文本属性
text
is an attribute of a WebElement 但不是方法。
因此您需要将text()
替换为text
,您的有效代码行将是:
title = links[i].text
只需将 text()
替换为 text
title = links[i].text