【问题标题】:Selenium Python Exception even though Selenium succeeds and clicks即使 Selenium 成功并单击,Selenium Python 异常
【发布时间】:2020-05-19 20:14:58
【问题描述】:

简而言之,在给出异常的代码成功后,我得到了一个异常。

def checkelementid(id1):
    try:
        second_driver.find_element_by_id(id1).click()
    except NoSuchElementException:
        return False
    except ElementNotInteractableException:
        return False
    return True

if checkelementid("requisitionDescriptionInterface.UP_APPLY_ON_REQ.row1"):
    print("before")
    second_driver.find_element_by_id("requisitionDescriptionInterface.UP_APPLY_ON_REQ.row1").click()
    print("after")

点击成功后出现以下错误,我转到一个新的网址:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="requisitionDescriptionInterface.UP_APPLY_ON_REQ.row1"]

所以它实际上找到了元素并点击并被带到一个新站点,但它以某种方式再次在新站点上运行点击,但显然无法找到该元素。它打印“之前”,但不打印“之后”。

【问题讨论】:

  • 您的代码告诉驱动程序查找具有该 ID 的元素,它找到它,然后单击它。然后浏览器加载一个新页面。驱动程序将等待页面加载完成。然后您尝试找到具有相同 id 的元素并单击它。此时,驱动程序会抛出元素未找到异常。

标签: python selenium


【解决方案1】:

尝试在 if 条件之前等待。

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

【讨论】:

    猜你喜欢
    • 2016-09-08
    • 2021-01-29
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2021-02-25
    • 2021-10-02
    • 1970-01-01
    相关资源
    最近更新 更多