【问题标题】:How to handle No SuchElementException in python automation,如何在 python 自动化中处理 No SuchElementException,
【发布时间】:2019-11-26 09:14:19
【问题描述】:
elem = driver.find_element_by_id("msgButtonAffirm")
if elem.is_displayed():
    elem.click()
    print("conform popup is avalable and click")
else:
    print "Pop-up is not visible"

【问题讨论】:

    标签: python selenium selenium-webdriver automation


    【解决方案1】:

    您可以使用find_elements_by_id 并检查列表中是否有任何内容

    elem = driver.find_elements_by_id("msgButtonAffirm")
    if elem and elem[0].is_displayed():
        elem[0].click()
        print("conform popup is avalable and click")
    else:
        print("Pop-up is not visible")
    

    【讨论】:

    • 您如何确定elem[0] 包含您正在寻找的所需元素?通过find_elements_by_id("msgButtonAffirm") 可以标识多个元素,所需的元素也可以是elem[2]、elem[3]。
    • @DebanjanB elem[0] 将包含相同的元素 find_element_by_id 将返回。
    • 没错,这就是我所说的,如此有效,使用elem[0] 与OP 的find_element_by_id() 相比没有任何优势
    • @DebanjanB 优点是find_elements_by_id 和elem[0] 不会抛出NoSuchElementException,如果没有出现弹出窗口,则OP 会收到find_element_by_id 会。
    • 仅处理 NoSuchElementException 并不能解决目的(读作用例),而是我认为我们需要相关的 HTML 以便元素可以被唯一地识别首先诱导 WebDriverWait (如果需要)然后继续。
    【解决方案2】:

    您可以导入异常并使用它,如下所示:

    from selenium.common.exceptions import NoSuchElementException
    
    try:
        elem = driver.find_element_by_id("msgButtonAffirm")
        elem.click()
        print("conform popup is avalable and click")
    except NoSuchElementException:  
        print("Pop-up is not visible")
    

    【讨论】:

      【解决方案3】:

      你需要注意几件事:

      • 在点击确认弹出窗口时,您不应该主要关注处理NoSuchElementException
      • 从历史上看,在大多数情况下,确认弹出窗口位于模态对话框中,因此您需要诱导WebDriverwait。

      相关的 HTML 将帮助我们以更好的方式分析问题。但是,根据上述几点,您需要将WebDriverWait 诱导为expected_conditions 为element_to_be_clickable(),您可以使用以下Locator Strategies 之一:

      • 使用CSS_SELECTOR:

        try:
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#msgButtonAffirm"))).click()
            print ("Pop-up was clickable and clicked")
        except TimeoutException: 
            print ("Pop-up was not clickable")
        
      • 使用XPATH:

        try:
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='msgButtonAffirm']"))).click()
            print ("Pop-up was clickable and clicked")
        except TimeoutException: 
            print ("Pop-up was not clickable")
        
      • 注意:您必须添加以下导入:

        from selenium import webdriver
        from selenium.common.exceptions import TimeoutException
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        

      【讨论】:

      • 如果弹出窗口永远不会出现怎么办?它会无缘无故地等待 20 秒。
      • @Guy 20 秒的 timespan 在这里只是一个示例,可以设置为任何值,例如3、5、7、10 等。实际上,timespan 应该根据测试计划进行配置。 Test Plan 应该定义 Modal Dialog Box、Angular 元素、JavaScript 的 timeout 持续时间i> 基于元素等
      • 关键不是它等待了多少时间,关键是它不必要地等待可能不存在的弹出窗口。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2021-03-29
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多