Home > other >  How to get text with Selenium WebDriver?
How to get text with Selenium WebDriver?

Time:08-07

I am new in selenium and web scrapping.

So, I am trying to get text from site specially made for web scrapping, and for some reason i can't do it.I am just get an error "'list' object has no attribute 'text". My code:

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)

Maybe my imports will help u to understand the reason:

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

CodePudding user response:

As find_elements returns a list of WebElement, you need to specify the Element index for which you want to access the text property (as there may be more elements with class=quote), that's why you are getting the 'list' object has no attribute 'text` error

You may want to use find_element_by_class_name() method in order to retrieve a single element, or if you want to get the n-th element with class=quote you may want to use an index on the List of Elements elements returned:

 elements = driver.find_elements(By.CLASS_NAME, 'quote')
 print(elements[2]) #get 3rd element with class=quote

If you want further information regarding Locating Elements using Selenium, see their documentation.

  • Related