How can login with selenium to website without defined ID?

Issue

I try to enter my university account to avoid filling out 7 surveys that are exactly the same and it does not matter what I answer. If I don’t fill them out manually, I can’t register my subjects and it’s the same every semester. I tried the following code trying to find the elements from the xpath, but it get Unable to locate element: {"method": "xpath", "selector": "// input [@type = 'text' and @ name = 'p_codigo_c'] "} (Session info: chrome = 92.0.4515.107)

How can i fix this?

Solution

Username and password fields are inside a frame. Switch to the frame before accessing the element inside it. Like this.

driver.switch_to_frame(driver.find_element_by_xpath("//frame[@name='mainFrame']"))

user_input = driver.find_element_by_xpath("//input[@type = 'text' and @name='p_codigo_c']")
user_input.send_keys(USERNAME)

password_input = driver.find_element_by_xpath("//input[@type = 'password' and @name='p_clave_c']")
password_input.send_keys(PASSWORD)

login_button = driver.find_element_by_xpath("//input[@type = 'submit' and @value='Ingresar']")
login_button.click()

Answered By – itronic1990

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published