1
I am trying to find a button using the webdriver + Python Selenium, the button code is:
<button class="w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ">
<div class="font-medium">
<div class="text-17 md:text-18 md:font-bold leading-18">Ativar</div>
<div class="text-13 md:text-12 font-normal md:font-medium leading-normal">4 horas Minerando</div>
</div>
</button>
I tried to:
driver.find_element_by_xpath('//button[.//div[text()="Ativar"]]').click()
But error message occurs:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[.//div[text()="Ativar"]]"}
However on the page if I search by inspecting>Crtl+F and paste
//button[.//div[text()="Ativar"]]
he finds the button... if anyone can help me please...
I particularly don’t like XPATH. A layout change on the screen and you lose your automation. Because you simply don’t use
driver.find_element_by_link_text('Ativar')
?– Paulo Marques
Try to inspect, inside the inspect element, in the selected item, right-click -> copy -> copy Xpath.. Try this way.
– Vinicius T
Want to understand how to find xpath from a button or know how xpath is extracted in the developer tool in the browsers but have doubt about the use in python lib?
– stack.cardoso