【问题标题】:Is it possible to grab a locator from webdriver exception text?是否可以从 webdriver 异常文本中获取定位器?
【发布时间】:2016-11-23 10:48:44
【问题描述】:

我在自动化一些测试时遇到了一些问题 - 主要是 WebDriverException,点击将被不同的对象捕获。我可以通过使用 webdriverwait 让元素消失来解决这个问题——它是在模式中显示的滑动成功消息,但异常消息让我思考;除了使用显式等待之外,是否可以捕获异常、解析文本并提取对象的一些可识别信息,然后在方法的 webdriverwait 中使用它?

例如,如果我这样做:

self.wait_for_success_modal_to_disappear(context)
self.element_is_clickable(context, mark_as_shipped).click()

它会工作,但如果我注释掉等待方法,它会失败并显示错误消息:

WebDriverException: Message: unknown error: Element is not clickable at
point (x, y). Other element would receive the click: <div class="success-modal">...</div>

我的想法是修改element_is_clickable 方法,在基于异常文本的可重用方法中包含异常处理,有点像这样:

def element_is_clickable(self, context, locator):
    try:
        WebDriverWait(context.driver, 15).until(
                    EC.visibility_of_element_located(locator)
                )
        WebDriverWait(context.driver, 15).until(
                    EC.element_to_be_clickable(locator)
                )
        return context.driver.find_element(*locator)
    except WebDriverException:
        error_message = repr(traceback.print_exc())
        modal_class_name = <<method to grab everything between the quotation marks>>
        WebDriverWait(context.driver, 15).until(
            EC.invisibility_of_element_located((By.CLASS_NAME, modal_class_name))
        )
        WebDriverWait(context.driver, 15).until(
                EC.visibility_of_element_located(locator)
            )
        WebDriverWait(context.driver, 15).until(
                EC.element_to_be_clickable(locator)
            )
        return context.driver.find_element(*locator)

现在,我知道这不是处理此问题的正确方法,因为错误在于 click() 而不是识别元素,但我最感兴趣的是捕获和解析异常消息的可能性和以有用的方式使用这些数据。这有可能吗?

【问题讨论】:

    标签: python selenium theory


    【解决方案1】:

    您可以等到可以捕获点击的对象消失,条件为until_not,例如

    import re
    
    try:
        # click already defined element, e.g. button
        element.click()
    except WebDriverException as e:
        # in case of captured click parse exception for this div locator
        xpath = '//div[@%s]' % re.search('class="\w+"', e.args[0]).group() # this is just a simple example of how to handle e.args[0] (exception message string) with regular expressions- you can handle it as you want to
        # wait until modal disappear
        WebDriverWait(driver, 15).until_not(EC.visibility_of_element_located((By.XPATH, xpath)))
        # click again
        element.click()
    

    【讨论】:

      猜你喜欢
      • 2017-01-27
      • 1970-01-01
      • 2015-11-14
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多