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

问题描述 投票:0回答:1

我正在尝试从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")>]

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

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的经验,也没有做很多刮刮操作,因此,如果这代表我是一个简单的误会,请向我道歉。

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

python selenium selenium-webdriver beautifulsoup geckodriver
1个回答
1
投票

将最后一行更改为:

© www.soinside.com 2019 - 2024. All rights reserved.