1

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • How can you access the element if the class name isn't unique? Maybe the class is unique with conjunction with e.g. h4 tag or text value? Is the DOM element is in the same place every time? Maybe you want to select it by text, i.e. by `Mis Comprobantes`? – Pawel Kam Dec 15 '21 at 13:27

1 Answers1

0

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']")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352