Selenium finding link by text when text-transform present

Question:

I have this HTML

<div class="tabsAction">
  <a class="butAction" href="" title="" data-original-title="">Validar</a>
  <a class="butAction" href="" title="" data-original-title="">Copiar</a>
  <a class="butAction" href="" title="" data-original-title="">Convertir en plantilla</a>
  <a class="butActionDelete" href="" title="" data-original-title="">Eliminar</a>
</div>

I’m trying to select the Validar link.

driver.find_element(By.LINK_TEXT, "Validar")

but selenium can’t find it.

However if I do this:

for link in links:
    print(link.text)

I get this:

VALIDAR
COPIAR
CONVERTIR EN PLANTILLA
ELIMINAR

I’ve checked and the class .butAction has a text-transform: uppercase; css.

I swear this 100% used to work just yesterday, why is it not working now? What am I missing?

Asked By: Daviid

||

Answers:

Instead of By.LINK_TEXT it’s better to use By.XPATH.
In this case you only need to know what text appears in the web element on the page, not how it presented to user.
This should work:

driver.find_element(By.XPATH, "//a[@class='butAction'][text()='Validar']")
Answered By: Prophet

I rarely use By.LINK_TEXT but I do remember a few times where it acts… odd in cases where the text is lowercase in the HTML but upper case on the page. Did you ever try all caps in your locator?

driver.find_element(By.LINK_TEXT, "VALIDAR")
Answered By: JeffC
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.