簡體   English   中英

如何使用 Selenium WebDriver 獲取文本?

[英]How to get text with Selenium WebDriver?

我是 selenium 和 web 報廢的新手。

所以,我試圖從專門為 web 報廢的站點獲取文本,但由於某種原因我不能這樣做。我只是得到一個錯誤“'list' object has no attribute 'text”。 我的代碼:

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
pages = 10

for page in range(1, pages):
    url = "http://quotes.toscrape.com/js/page/" + str(page) + "/"
    driver.get(url)

    items = len(driver.find_elements(By.CLASS_NAME, 'quote'))
    total = []
    for item in range(items):
        quotes = driver.find_elements(By.CLASS_NAME, 'quote').text
        print(quotes)

也許我的導入會幫助你理解原因:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

由於find_elements返回WebElement 列表,您需要指定要訪問其text屬性的元素,這就是為什么您得到“列表” object has no attribute 'text`錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM