How to find element by visible text using selenium?

Viewed 41

I'm trying to find a text and click, but I don't know what's going on nothing that I try is working.

I've already tried:

    driver.find_element(By.XPATH("//*[contains(text(),'Retenção colocada pelo sistema para reter documentos fiscais que requerem confirmação eletrônica. ')]")).click()

and

driver.find_elements_by_xpath("//*[contains(text(), 'Retenção colocada pelo sistema para reter documentos fiscais que requerem confirmação eletrônica. ')]")

I get this error: 'str' object is not callable

2 Answers

Instead of driver.find_element(By.XPATH("//*[contains(text(),'Retenção colocada pelo sistema para reter documentos fiscais que requerem confirmação eletrônica. ')]")).click() Try

driver.find_element(By.XPATH, "//*[contains(text(),'Retenção colocada pelo sistema para reter documentos fiscais que requerem confirmação eletrônica.')]").click()

If you are searching by partial text - the best practice will be to ignore characters like dot (.), semi-colon (;) or apostrophe (').

 driver.find_element(By.XPATH, "//*[contains(text(),'Retenção colocada pelo sistema para reter documentos fiscais')]").click()

Also ignore some of the words, up to 8 or 9 words is more than enough.

Related