I am trying to fill a form with selenium, from this website https://www.kartingbowling.com/vannes/page-comite-d-entreprise-22.html
here is the code :
code = 1010
bouton = web.find_element_by_xpath('//*[@id="ticketce"]')
bouton.click()
time.sleep(2)
form = web.find_element_by_xpath('/html/body/div/div/form/input[1]')
form.click()
form.send_keys(str(code))
it firsts clicks on "testez la validité de votre ticket CE" then I want to fill the small box and click "verifier"
however, when I try it I get this exception at line 5 :
"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/form/input[1]"}"
anybody knows how to fix this?
thanks
CodePudding user response:
You are getting this exception
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/form/input[1]"}
cause element is in iframe and you have not switched to it, and trying to access it.
Please switch to iframe first and then you can access the element.
code = "1010"
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.kartingbowling.com/vannes/page-comite-d-entreprise-22.html")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#ticketce"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.fancybox-iframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#code"))).send_keys(code)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#btn"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC