Selenium: 'list' 对象没有属性 'text'

Selenium: 'list' object has no attribute 'text'

我正在尝试从 Google 搜索结果页面中获取元素 id='resultStats' 中的文本,用于以下查询 https://www.google.com/search?q=site:https://theshipibomarket.com/

我得到了一个带有代码的输出,但它不是元素中的文本。输出是: [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="5a1e4063-dcb8-48b2-93f6-1c60bb7e9e05", element="63dabd48-bd5f-4380-9598-173b91e72367")>]

当我在 results 元素上使用 .text 函数时,出现以下错误:

AttributeError: 'list' object has no attribute 'text'

这是我的代码:

# import libraries
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.headless = True

query = "site:https://theshipibomarket.com/"
urlpage = "https://www.google.com/search?q="+query
print(urlpage)
# run firefox webdriver from executable path of your choice
driver = webdriver.Firefox(options=options)
# get web page
driver.get(urlpage)
# execute script to scroll down the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
# sleep for 30s
time.sleep(30)
# driver.quit()

# find elements by xpath
results = driver.find_elements_by_xpath("//*[@id='resultStats']")
#print('Number of results', len(results))
print("The number of pages Google have index {}".format(results.text))

我怀疑是 javascript 导致了这个问题,因为输出是 list。我没有太多抓取 Google 的经验,也没有做很多一般的抓取,所以如果这是一个简单的误解,请代表我道歉。

将最后一行更改为:

print("The number of pages Google have index {}".format(results[0].text)) # added zero