Finding selenium by class attribute when classname isn't unique using Selenium

Viewed 54

How I can select Element by class attribute?

I have:

<h4 class="bold " >Mis Comprobantes</h4>

I need to click in Mis Comprobantes, I can't use class name because it isn't unique.

1 Answers

Incase the classname i.e. bold isn't unique you need to use other attributes or club up the classname with some other attributes to construct a unique Locator Strategy.

To identify the following element:

<h4 class="bold " >Mis Comprobantes</h4>

You can use either of the following Locator Strategies:

  • Using the text:

    element = driver.find_element(By.XPATH, "//h4[text()='Mis Comprobantes']")
    
  • Using the class attribute and the text __:

    element = driver.find_element(By.XPATH, "input[@class='bold ' and text()='Mis Comprobantes']")
    
Related