¿Cómo evitar NoSuchElementException en selenio? [duplicar]
Aug 18 2020
Soy nuevo en todo esto, así que pido disculpas si es una pregunta tonta.
aquí está todo lo que estoy tratando de hacer:
nxt_btn = driver.find_element_by_xpath('/html/body/main/article/section/form/div[2]/button')
nxt_btn.click()
time.sleep(2)
vote_check = driver.find_element_by_xpath('/html/body/main/article/section/form/div[1]/div[2]/div/div/fieldset/div/div/div[2]/div/input')
vote_check.click()
time.sleep(2)
nxt_btn = driver.find_element_by_xpath('/html/body/main/article/section/form/div[2]/button')
nxt_btn.click()
driver.quit()
y devuelve:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/article/section/form/div[1]/div[2]/div/div/fieldset/div/div/div[2]/div/input"}
(Session info: chrome=84.0.4147.125)
Intenté cambiar el tiempo de espera, elegir diferentes elementos y verificar si está en un iframe, y si lo está, no puedo encontrarlo simplemente subiendo por el camino.
Respuestas
1 AmrutaPande Aug 18 2020 at 12:18
En lugar de dormir, use las esperas explícitas de Selenium
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "XPATH"))
Las condiciones que puede utilizar son:
- visibilidad_de_elemento_ubicado
- texto_a_estar_presente_en_elemento
- element_to_be_clickable