1

Im trying to press a button using selenium but the ID is generated every time I access the website. I've tried using the xpath and Css selectors that I get from Google Chrome but it appears to be using the ID so its unreliable. Heres the Html Code

<ul data-componentname="gender">

  <li id="4ed02d40-a727-4c69-9bb6-b39bcb752bfc" class="">
    <input type="button">
    <span>Male</span>
  </li>

  <li id="76e7055e-ac37-4d12-bec3-27f9ca3410c0" class="">
    <input type="button">
    <span>Female</span>
  </li>

1 Answer 1

1

To Click on Male or Female button induce WebDriverWait and element_to_be_clickable and following xpath

To click on Male:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@data-componentname='gender']//li[./span[text()='Male']]/input[@type='button']"))).click()

To click on Female:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@data-componentname='gender']//li[./span[text()='Female']]/input[@type='button']"))).click()

Note: You need to imports followings.

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

EDITED

elementmale=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@data-componentname='gender']//li[./span[text()='Male']]/input[@type='button']")))
driver.execute_script("arguments[0].click();", elementmale)
Sign up to request clarification or add additional context in comments.

2 Comments

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="button"> is not clickable at point (684, 332). Other element would receive the click: <span>...</span> (Session info: chrome=76.0.3809.132)
Use JavaScript executor to click on this.check my updated code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.