在网站的登录覆盖表单中输入用户名和密码的脚本。用户名字段可以通过 XPATH 找到并输入文本,密码字段无法通过 XPATH 或 ID 找到,导致“TimeoutException”错误。 可以通过“password_field = driver.find_element(By.ID, 'password')”找到密码字段,但无法输入文本,导致“ElementNotInteractableException”错误。
username_field = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="email"]')))
username_field.send_keys(username)
password_field = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="password"]')))
#password_field = driver.find_element(By.ID, 'password')
password_field.send_keys(password)
sign_in_button = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="login-form"]')))
sign_in_button.click()
尝试只输入一个字符“1”作为密码,在send_keys之前添加password_field.clear()和password_field.click()。任何想法?谢谢大家。
确保根据HTML结构调整定位器策略
sername_field = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="email"]')))
username_field.send_keys(用户名)
尝试: password_field = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, '密码'))) #password_field = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, '密码')))
sign_in_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="login-form"]'))) 登录按钮.click()