Software Development

How can I iterate through a list of WebElements to print text from elements with the same class?

KI Asked by Kimberly Evans · 14-06-2024
0 upvotes 15,229 views 0 comments
The question

I am trying to scrape a list of product names from a search results page. All the names share the same class attribute, "product-title". I’ve successfully used find_elements to grab the list, but when I try to print it, I get a list of object references instead of the actual text. What is the correct loop structure in Selenium-Python to extract and print the visible text for each element?

3 answers

0
BA
Answered on 22-08-2024

The reason you are seeing object references is that find_elements (plural) returns a Python list of WebElement objects, not their content. To get the text, you must iterate through this list using a for loop and access the .text property of each individual object. For example: elements = driver.find_elements(By.CLASS_NAME, "product-title"). Then, use for element in elements: print(element.text). This is a fundamental concept in Selenium: the driver returns a handle to the DOM element, and you must then query that handle for specific attributes like text, value, or CSS properties. If some elements are not visible, .text might return an empty string; in such cases, you can use element.get_attribute("textContent") to retrieve the text even from hidden elements.

0
CH
Answered on 12-09-2024

Does your script handle the case where the page uses lazy loading, or do you find that the list of elements is empty if you don't wait for the page to fully render?

MA 15-09-2024

Christopher, that’s a critical point for reliability. If the class names are generated dynamically or the content is loaded via AJAX, a standard find_elements might execute before the items are present. I always recommend using WebDriverWait in combination with visibility_of_all_elements_located. This ensures the script pauses until at least one element with that class is visible, which prevents the loop from executing on an empty list and significantly reduces flakiness in automated testing environments.

0
JE
Answered on 15-11-2024

You can also use a list comprehension for a cleaner one-liner: texts = [el.text for el in driver.find_elements(By.CLASS_NAME, "your-class")]. It's very Pythonic and efficient!

KI 18-11-2024

I agree with Jessica. List comprehensions are excellent for data extraction because they are faster and make the code much more readable. I often use this approach and then immediately join the list into a string or export it to a CSV file for further analysis in data science projects.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session