webdriver 忽略某些页面上的等待

webdriver ignoring waits on certain pages

我有一种情况,我正在用 selenium webdriver 测试一些东西。当尝试登录 OneDrive 时,驱动程序忽略所有等待,我得到 "element not visible error",特别是针对您输入密码的页面。这只发生在这种情况下,其余情况我使用几乎相同的代码 运行 多个页面上的登录过程工作正常。

这是失败代码对应的代码

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 版本,它 运行 没问题,但它不稳定并且随机 "connection ended remotedly"

我注意到它没有加载新页面,而是动态更改表单的内容以显示每个步骤的不同字段。不确定如何正确处理它,所以我不得不使用 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()