0

I am using Python 3 and Selenium(Chromedriver). I want to check for a element with this command.

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button"))).click()

The problem is that the XPATH is constantly changing between two paths:

/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
/html/body/div[3]/div/div[3]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button

I want to tell Python, that if the element is not found, it should search for the other XPATH.

If you know a method to find the element without the XPATH, i would also be happy with the solution. It does not work if you search for the elemnt by its containing text, because the language from the sites changes if use a proxy. This is the "Inspect Element" code of the button:

<button aria-label="Mobilnummer hinzufügen" class="bg-white css-1eajgu7 ex41m6f0 btn-secondary-dark " type="button">Hinzufügen</button>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Marten
  • 645
  • 7
  • 21
  • Try `(By.CLASS_NAME, "btn-secondary-dark")` – JaSON Sep 10 '20 at 14:32
  • No, does not work. Your code selects the first button which is found. Still if there was only one button, it would be a good solution. Thanks! – Marten Sep 10 '20 at 15:17
  • add some more HTML so we can include ancestors in selector – JaSON Sep 10 '20 at 15:40
  • Hey, you can access the site with a username and password which I have provided at the end of this question: https://stackoverflow.com/questions/63784036/selenium-common-exceptions-timeoutexception-while-clicking-on-a-button-using-exp The button "Add Phone number" is the button I mean. – Marten Sep 10 '20 at 15:45

2 Answers2

0

To click on the element with text as Hinzufügen you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//button[@aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hey, thanks for your answer, but that wont work because you used the language-keywords. :) Still thanks! – Marten Sep 10 '20 at 15:22
0

Try to use this XPath:

//div[contains(@class, 'mex-mobile-phone')]//button[contains(@class, 'btn-secondary-dark')]
JaSON
  • 4,843
  • 2
  • 8
  • 15
  • I am getting this error: Traceback (most recent call last): File "C:/Users/Marten/PycharmProjects/NikeSNKRS/testdelete.py", line 228, in WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'mex-mobile-phone')]//button[contains(@class, 'btn-secondary-dark')]"))).click() File "C:\Users\Marten\PycharmProjects\NikeSNKRS\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Marten Sep 10 '20 at 17:11
  • @MatrixSpielt Hmm... Works fine for me. Do you mean this page https://www.nike.com/de/member/settings, right? – JaSON Sep 11 '20 at 08:35
  • I have completely rewritten my code. The problem was that the IDs were constantly changing. – Marten Sep 12 '20 at 12:04