estebaw55
estebaw55

Reputation: 1

How to click on this button with Selenium

HTML:

<button type="submit" class="button primary g-recaptcha" 
data-sitekey="6LdSmG4cAAAAAAOarRxGIhehvv4sPgVeF-vRi-Jqb" 
data-callback="onSubmit">Avanti</button>

Attempt 1:

WebDriverWait(driver, 20)
.until(EC.element_to_be_clickable((By.XPATH, 
"/html/body/main/div/section[1]/form/button")))
.click()

Attempt 2:

WebDriverWait(driver, 20)
.until(EC.element_to_be_clickable((By.XPATH, 
"//[@class='button primary g-recaptcha' and @type='submit']")))
.click()

Upvotes: 0

Views: 216

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

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.button.primary.g-recaptcha[data-callback='onSubmit']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='button primary g-recaptcha' and text()='Avanti']"))).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
    

Update

Presumably, it's a recaptcha element and generally they resides within iframes. So you need to switch_to.frame() first as follows:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"id_of_iframe"))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button.primary.g-recaptcha[data-callback='onSubmit']"))).click()

Upvotes: 2

Related Questions