登录表单提交失败而没有引发错误

时间:2018-09-01 05:30:47

标签: python html selenium

仅意识到尽管没有引发错误,也没有提交登录表单:

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代码-Onlne编码器-医疗账单和编码”和页面源的其余部分确认登录失败

我在调用.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: 

为此post,我还尝试在调用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'

非常感谢任何指针。

1 个答案:

答案 0 :(得分:1)

在下面使用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()