Python webdriver忽略某些页面上的等待
我的情况是,我正在用SeleniumWebDriver测试一些东西。当尝试登录OneDrive时,驱动程序会忽略所有等待,我会得到“element not visible error”,特别是针对您输入密码的页面。只有在这种情况下才会发生这种情况,在其他情况下,我使用几乎相同的代码在多个页面上运行登录过程,效果良好 这是与失败代码对应的代码Python webdriver忽略某些页面上的等待,python,selenium,selenium-webdriver,wait,pytest,Python,Selenium,Selenium Webdriver,Wait,Pytest,我的情况是,我正在用SeleniumWebDriver测试一些东西。当尝试登录OneDrive时,驱动程序会忽略所有等待,我会得到“element not visible error”,特别是针对您输入密码的页面。只有在这种情况下才会发生这种情况,在其他情况下,我使用几乎相同的代码在多个页面上运行登录过程,效果良好 这是与失败代码对应的代码 def selenium_onedrive(loading_done_event, selenium, user, psw): loading_done_e
def selenium_onedrive(loading_done_event, selenium, user, psw):
loading_done_event.wait()
login = selenium.find_elements_by_name('loginfmt')[0]
login.send_keys(user)
next_step = selenium.find_element_by_id('idSIButton9')
next_step.click()
password = WebDriverWait(selenium, 10).until(
# EC.presence_of_element_located((By.NAME, "passwd"))
EC.element_to_be_clickable((By.ID, "i0118"))
)
**password.send_keys(psw)**
# password.submit()
next_step = selenium.find_element_by_id('idSIButton9')
next_step.click()
粗体线是错误出现的地方。它说找不到元素,但等待(甚至是隐式的)被忽略
这是一个有效的登录代码示例
def selenium_gdrive(loading_done_event, selenium, user, psw):
loading_done_event.wait()
login = selenium.find_elements_by_name('Email')[0]
login.send_keys(user)
selenium.find_elements_by_name('signIn')[0].click()
password = WebDriverWait(selenium, 10).until(
EC.presence_of_element_located((By.NAME, "Passwd"))
)
password.send_keys(psw)
password.submit()
# now we will be navigated to the consent page
consent_accept_button = WebDriverWait(selenium, 10).until(
EC.element_to_be_clickable((By.ID, "submit_approve_access"))
)
consent_accept_button.click()
其他信息,使用Firefox驱动程序运行代码。如果我使用Chrome版本,它运行正常,但不稳定,会随机出现“连接远程终止”我注意到它没有加载新页面,而是动态更改表单内容,以显示每个步骤的不同字段。我不知道如何正确处理这个问题,所以我不得不使用time.sleep(1)等待加载内容和代码定位新元素。我知道这不是最好的办法,但现在是我找到的唯一解决办法 最终代码
def selenium_onedrive(loading_done_event, selenium, user, psw):
loading_done_event.wait()
login = selenium.find_elements_by_name('loginfmt')[0]
login.send_keys(user)
next_step = selenium.find_element_by_id('idSIButton9')
next_step.click()
time.sleep(1)
password = WebDriverWait(selenium, 10).until(
# EC.presence_of_element_located((By.NAME, "passwd"))
EC.element_to_be_clickable((By.ID, "i0118"))
)
**password.send_keys(psw)**
# password.submit()
next_step = selenium.find_element_by_id('idSIButton9')
next_step.click()
哪一行对此负责?错误出现在“password.send_keys(psw)”这一行,它表示找不到元素。它完全忽略了等待,即使我隐式地使用了_wait()