Selenium how to locate all elements with the tag h2


Hello so given that i have navigated to this site

I would like to retrieve the headings of each catogory by using inspect element, I noticed that every heading of the catorgories has a h2 tag

enter image description here

My thoughts were to use a EC condition to wait till for the visibility of all elements that have a h2 tag which should return a list containing all h2 tags which i can then just call getattribute(text) on to get the text

def select_catogory(self):
        list = self.wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'h2')))
        catogories = self.driver.find_elements(By.TAG_NAME,value ="h2")
        for cat in catogories:

both presence_of_all_elements_located returned nothing and same with find_elements


I realised that my main issue was in my loop where i never had a break condition so it was giving a stale element warning, which i thought was caused by this function, Thanks for the help

def select_button_by_name(self, name_of_button: str):
        Navigates to the button in question and clicks on it

        input: name_of_button (string)
        buttons_by_href = self.driver.find_elements(By.XPATH,value ="//a[@href]") #finds all the href tags on the webpage
        for button in buttons_by_href:
           # Matches the attribute text to the provided button string
            if button.get_attribute("text") == name_of_button:
                #clicks on the button
Asked By: Darman



This SeleniumBase solution worked:

pip install seleniumbase, then run with python or pytest:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__, "-s")

class MyTestClass(BaseCase):
    def test_base(self):"")
        h2_elements = self.find_visible_elements("h2")
        for element in h2_elements:

Output was:

Animals & Pets
Beauty & Well-being
Business Services
Construction & Manufacturing
Education & Training
Electronics & Technology
Events & Entertainment
Food, Beverages & Tobacco
Health & Medical
Hobbies & Crafts
Home & Garden
Home Services
Legal Services & Government
Media & Publishing
Money & Insurance
Public & Local Services
Restaurants & Bars
Shopping & Fashion
Travel & Vacation
Vehicles & Transportation
Answered By: Michael Mintz

You can extract the headings of each catogory using Selenium and in a single line of code inducing WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute("textContent"):

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
    print([my_elem.get_attribute("textContent") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "h2[class^='typography_heading']")))])
  • Using XPATH and text attribute:

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
    print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//h2[text()]")))])
  • Console Output:

    ['Animals & Pets', 'Beauty & Well-being', 'Business Services', 'Construction & Manufacturing', 'Education & Training', 'Electronics & Technology', 'Events & Entertainment', 'Food, Beverages & Tobacco', 'Health & Medical', 'Hobbies & Crafts', 'Home & Garden', 'Home Services', 'Legal Services & Government', 'Media & Publishing', 'Money & Insurance', 'Public & Local Services', 'Restaurants & Bars', 'Shopping & Fashion', 'Sports', 'Travel & Vacation', 'Utilities', 'Vehicles & Transportation']
  • Note : You have to add the following imports :

    from import WebDriverWait
    from import By
    from import expected_conditions as EC
Answered By: undetected Selenium