首页 > 解决方案 > 如何在没有定义 ID 的情况下使用 selenium 登录网站?

问题描述

我尝试输入我的大学帐户以避免填写 7 个完全相同的调查,而我回答的内容并不重要。如果我不手动填写,我就无法注册我的科目,而且每个学期都一样。我尝试了以下代码试图从 中查找元素xpath,但它得到了Unable to locate element: {"method": "xpath", "selector": "// input [@type = 'text' and @ name = 'p_codigo_c'] "} (Session info: chrome = 92.0.4515.107)

我怎样才能解决这个问题?

标签: pythonselenium

解决方案


用户名和密码字段位于框架内。在访问其中的元素之前切换到框架。像这样。

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()

推荐阅读