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?
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']")
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")
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?
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']")
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")