Search code examples
python-3.xseleniumclick

Selenium not checking box


Trying to get selenium to check a box with python but it seems to keep timing out.

Current code is

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

driver = webdriver.Chrome()
driver.get('https://grabagun.com/giveaway')

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="terms_and_conditions"]'))).click()

The error is:

InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[@id="terms_and_conditions" because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@id="terms_and_conditions"' is not a valid XPath expression.

Any suggestions?


Solution

  • You could use action chains and the correct lookup:

    from selenium.webdriver.common.action_chains import ActionChains
    
    element = driver.find_element(By.ID, "terms_and_conditions")
    
    ActionChains(driver).move_to_element(element).click().perform()