如何在Selenium Safari WebDriver中禁用自动关闭意外警报

时间:2019-04-25 08:29:27

标签: selenium safari webdriver python-3.6 alert

在Selenium Safari WebDriver中引发UnexpectedAlertPresentException时,我想收到一条警报消息。但是,我不能这样做,因为当它升高时,Safari WebDriver会自动关闭警报。结果,警报窗口关闭,我遇到了NoAlertPresentException。

使用完全相同的代码与Chrome webdriver一起工作。我试图在下面设置desirable_capabilities。但这不起作用。

caps = DesiredCapabilities.SAFARI.copy()
caps['handlesAlerts'] = True
caps['unexpectedAlertBehaviour'] = 'ignore'


driver = webdriver.Safari(
    executable_path='/usr/bin/safaridriver',
    desired_capabilities = caps
)

try:
    driver.get(url)
    if browser == 'ie':
        fullpage_screenshot(driver, screenshot_name)
    else:
        save_screenshot(driver, screenshot_name)

except UnexpectedAlertPresentException:
    print('Screenshot Error:', UnexpectedAlertPresentException)
    if browser == 'ie':
        continue

        try:
            WebDriverWait(driver, 5).until(
                EC.alert_is_present(),
                'Timed out waiting for alert to appear'
            )                     
            alert = driver.switch_to_alert()
            memo[idx] = memo[idx] + str(alert.text)                        
            print('Alert Message:', alert.text)
            alert.accept()

            if not os.path.isdir('./errors/'):
                os.mkdir('./errors/')
            driver.get_screenshot_as_file('./errors/Error '+ screenID[idx] +'_'+ browser +'.png')

            memo[idx] = str(memo[idx]) + '\n' + str(UnexpectedAlertPresentException)                    
            continue

        except TimeoutException:
            print('No alert')
            continue

        except Exception as e:
            print('Inner Exception', e)
            continue

except Exception as e:
    memo[idx] = memo[idx] + str(e)
    print('Exception: ', e)
    continue

这是我期望的结果:

Screenshot Error: <class 'selenium.common.exceptions.UnexpectedAlertPresentException'>
Alert Message: <ERROR><![CDATA[No Data.]]></ERROR>

这是在Safari WebDriver中引发UnexpectedAlertPresentException时的实际结果。

Screenshot Error: <class 'selenium.common.exceptions.UnexpectedAlertPresentException'>
No alert
Screenshot Error: <class 'selenium.common.exceptions.UnexpectedAlertPresentException'>
No alert

0 个答案:

没有答案