[英]Selenium: 'list' object has no attribute 'text'
I'm trying to get the text within the element id='resultStats'
from the Google Search Result Page for the following query https://www.google.com/search?q=site:https://theshipibomarket.com/我正在尝试从 Google 搜索结果页面获取元素id='resultStats'
中的文本以获取以下查询https://www.google.com/search?q=site:https://theshipibomarket.com/
I get an output with the code I've got but it's not the text within the element.我得到一个 output 的代码,但它不是元素中的文本。 The output is: [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="5a1e4063-dcb8-48b2-93f6-1c60bb7e9e05", element="63dabd48-bd5f-4380-9598-173b91e72367")>]
The output is: [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="5a1e4063-dcb8-48b2-93f6-1c60bb7e9e05", element="63dabd48-bd5f-4380-9598-173b91e72367")>]
When I use the .text
function on the results
element I get the follow error:当我在results
元素上使用.text
function 时,出现以下错误:
AttributeError: 'list' object has no attribute 'text'
Here's my code:这是我的代码:
# 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))
I suspect that because it's javascript that this is causing the issue, as the output is a list
.我怀疑是因为 javascript 导致了这个问题,因为 output 是一个list
。 I've not got much experience scraping Google or doing a lot of scraping in general so apologise if this is a simple misunderstanding on my behalf.我没有太多刮谷歌的经验,或者一般来说做很多刮,所以如果这是代表我的一个简单的误解,我深表歉意。
Change the last line to:将最后一行更改为:
print("The number of pages Google have index {}".format(results[0].text)) # added zero
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.