Find a button using Xpath Selenium

Asked

Viewed 326 times

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...

  • 4

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

  • Try to inspect, inside the inspect element, in the selected item, right-click -> copy -> copy Xpath.. Try this way.

  • 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?

3 answers

4

Avoid using the find_element_by_xpath, because any modification in the layout page will break your script.

Give preference to:

  • find_element_by_id
  • find_element_by_name
  • find_element_by_class (not always valid, because different elements can share the same class)

However, as I did not have access to the entire page source, in the specific case, I would use the find_element_by_link_text as below

elem = driver.find_element_by_link_text('Ativar')
elem.click()

0

If this is the only button on the page, you can try something like this:

driver.find_element_by_xpath(//*button[@class='font medium']descedant-or-self::div[@class='text-17 md:text-18 md:font-bold leading-18').click();

If it doesn’t work that way, I suggest two modifications:

  1. Try to take the asterisk I put before the button.
  2. Replace the "descedant-or-self::" with a single bar "/"

I also believe it will help you link to learn a few tricks when searching for Xpath.

0

Hello, I believe that you need to wait for the page to load and only when the load is finished to search for the element by XPATH. Follow the example, not tested, but should work for what you need.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

# Sua lógica anterior

# Essa parte do código vai esperar até o botão aparecer na tela
WebDriverWait(driver, 3).until(
  expected_conditions.visibility_of_element_located(
    (By.XPATH, '//button[.//div[text()="Ativar"]]')
  )
)
  • And as @Paulo Marques said, avoid using XPATH.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.