0

Estoy empezando con Selenium y Python. Tengo problema de acceso a campo.

Código de acceso:

s=Service('C:\Drivers\chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://XXXXXXXXXXXXXXX'
browser.get(url)
browser.maximize_window()
#browser.find_element(By.ID, "//input[contains(@id,'mat-input-0')]")
#browser.find_element(By.CSS_SELECTOR, "//input[contains(@id,'mat-input-0')]")
browser.find_element(By.XPATH, "//input[contains(@id,'mat-input-0')]")

No se donde estoy fallando. ¿Es que el XPATH: "//input[contains(@id,'mat-input-0')]" no es correcto?

Este es el error:

introducir la descripción de la imagen aquí

Así es como obtengo el Xpath:

introducir la descripción de la imagen aquí

4
  • Prueba a no usar el XPath relativo, intenta usar el absoluto
    – J-Tron123
    el 4 abr. 2022 a las 10:42
  • Nada, me sigue saltando el error: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html[1]/body[1]/gc-root[1]/gc-plain-layout[1]/gc-login[1]/div[1]/div[2]/div[1]/mat-card[1]/div[1]/div[2]/gc-login-form[1]/div[1]/mat-card[1]/form[1]/mat-card-content[1]/gc-input[1]/mat-form-field[1]/div[1]/div[1]/div[1]/input[1]"} el 4 abr. 2022 a las 11:06
  • El acceso al elemento lo haria asi: browser.find_element(By.XPATH, "/html[1]/body[1]/gc-root[1]/gc-plain-layout[1]/gc-login[1]/div[1]/div[2]/div[1]/mat-card[1]/div[1]/div[2]/gc-login-form[1]/div[1]/mat-card[1]/form[1]/mat-card-content[1]/gc-input[1]/mat-form-field[1]/div[1]/div[1]/div[1]/input[1]") el 4 abr. 2022 a las 11:09
  • Puede ser que como estás buscando por XPath relativo y en el método llamas por XPath a secas puede que te esté lastrando
    – J-Tron123
    el 4 abr. 2022 a las 14:31

1 respuesta 1

0

Puede ser que Selenium se ejecute antes que la pagina cargue.

Agrega un time.sleep(1) antes de llamar al XPATH para que espere un segundo.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.