Get the text from multiple elements with the same class in Selenium for Python?


We can get text from multiple elements with the same class in Selenium webdriver. We have to use find_elements_by_xpath(), find_elements_by_class_name() or find_elements_by_css_selector() method which returns a list of all matching elements.

Syntax −

l=driver.find_elements_by_class_name("gsc-input")

Next we shall get the size of the list with len method. We shall iterate through this list and obtain the text with text method.

Example

Code Implementation.

from selenium import webdriver
driver = webdriver.Chrome (executable_path="C:\chromedriver.exe")
# maximize with maximize_window()
driver.maximize_window()
driver.get("https://www.justdial.com/Bangalore/Bakeries")
# identify elements of same classname
l=driver.find_elements_by_class_name("store-name")
# iterate through list and get text
for i in l:
   print("Store names:"+ i.text)
driver.close()

Output

Updated on: 28-Aug-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements