【问题标题】:webdriver ignoring waits on certain pageswebdriver忽略某些页面上的等待
【发布时间】:2017-01-23 16:12:58
【问题描述】:

我有一种情况,我正在使用 selenium webdriver 测试一些东西。尝试登录 OneDrive 时,驱动程序会忽略所有等待,并且我收到“元素不可见错误”,特别是针对您输入密码的页面。这只发生在这种情况下,我使用几乎相同的代码在多个页面上运行登录过程的其余实例都可以正常工作。

这是失败代码对应的代码

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 版本,它运行良好,但不稳定并随机出现“远程连接结束”

【问题讨论】:

  • 哪条线路负责?
  • 在“password.send_keys(psw)”这一行中抛出错误,它表示找不到元素。它完全忽略了等待,即使我使用implicitly_wait()

标签: python selenium selenium-webdriver wait pytest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2012-07-07
    • 2017-02-21
    • 2014-05-20
    • 2018-08-13
    • 2021-03-26
    相关资源
    最近更新 更多