python - 登录表单提交失败而没有抛出错误
问题描述
刚刚意识到尽管没有抛出错误,但没有提交登录表单:
from selenium import webdriver
driver_path = "path to chromedriver.exe"
url_login = "https://www.findacode.com/signin.html"
username = 'jd@mailinator.com'
password = 'm%$)-Y95*^.1Gin+' #know it's not best practice to share passwords, but this is a trial account and credentials are necessary to appreciate the problem
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)
driver.get(url_login)
form = driver.find_element_by_name('login')
form.find_element_by_name('id').send_keys(username)
form.find_element_by_name('password').send_keys(password)
form.find_element_by_xpath("//input[@value='Sign In']").submit()
此时没有错误,但登录不成功:
driver.title
是“登录 - FindACode.com”,但应该是“查找代码 - ICD 10 代码、CPT 代码、HCPCS 代码、ICD 9 代码 - 在线编码器 - 医疗账单和编码”,并且页面源的其余部分确认登录失败
我在调用 .submit() 后尝试了显式等待:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait(driver, 10).until(EC.visibility_of_element_located((By.ID, "history"))) # history is an element in post login landing page but not in the pre login page
但我收到超时错误:
Traceback (most recent call last):
File "<input>", line 29, in <module>
File "...\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
我在调用 .submit() 后尝试了另一个显式等待
title = "Find-A-Code - ICD 10 Codes, CPT Codes, HCPCS Codes, ICD 9 Codes - Onlne Encoder - Medical Billing and Coding"
wait(driver, 10).until(EC.title_is(title))
我收到另一个超时错误:
Traceback (most recent call last):
File "<input>", line 30, in <module>
File "...\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
根据这篇文章,我还尝试在调用 send_keys(password) 之前等待,以防密码字段变得陈旧
wait.until(EC.staleness_of((By.NAME, "password")))
但是我似乎无法正确使用语法,并且文档也没有帮助:
Traceback (most recent call last):
File "<input>", line 26, in <module>
TypeError: until() missing 1 required positional argument: 'method'
任何指针都非常感谢。
解决方案
在下面使用 css 定位器:
driver.find_element_by_css_selector('form[name=login] input[name=id]').send_keys(username)
driver.find_element_by_css_selector('form[name=login] input[name=password]').send_keys(password)
driver.find_element_by_css_selector('form[name=login] input[type="submit"]').click()
推荐阅读
- python - ORM SQL 查询未检索到 SQLAlchemy 文本 SQL 更新
- python - TensorFlow 模型无法在服务器上训练(“无法找到...的源代码”;脚本在不同的机器上运行良好)
- javascript - 使用 JsZip 在 wordpress 网站上下载 Accordion multi-zip
- javascript - 参考错误:RiLexicon 未定义
- javascript - 如何使用 --constructor-args 参数运行 Hardhat?
- c# - 更改数据网格单元格颜色
- php - 具有多个扩展实体的 Symfony 唯一实体 ORM 错误别名
- react-native - 我正在尝试以 react-native 修复屏幕底部的标签栏,在输入一些文本时
- c# - 从两个来源创建客户对象
- java - 使用拆分 EIP 进行并行处理时管理线程数