chromedriver无头警报 [英] chromedriver headless alerts

查看:118
本文介绍了chromedriver无头警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用chromedriver进行硒webdriver测试时遇到了这个问题.尽管使用Chrome浏览器可以成功运行测试,但无法在无头模式下运行相同的测试.

I have this issue with selenium webdriver tests with chromedriver. Although I can run tests succesfully when using Chrome browser I can't run the same tests in headless mode.

我无法处理Js警报.实际上,在截屏时似乎没有弹出警报.

I cannot handle the Js alerts. Actually when taking a screenshot it seems that the alert won't even pop-up.

提醒屏幕截图

我尝试了几种解决方法:

I have tried several workarounds:

1)driver.window_handles->似乎没有其他窗口

1) driver.window_handles --> No other window seems to be present

2)driver.execute_script("window.confirm = function(){return true;}")->该脚本没有任何改变

2) driver.execute_script("window.confirm = function(){return true;}") --> Nothing changed with that script

3)element = WebDriverWait(driver, 20).until(EC.alert_is_present())当然还有明确的等待

3) element = WebDriverWait(driver, 20).until(EC.alert_is_present()) and of course an explicit wait

在浏览器模式下,我使用纯文本:

In browser mode I use a plain:

try:
    print driver.switch_to.alert.text
    driver.switch_to.alert.accept()
except NoAlertPresentException as e: 
    print("no alert")

还有其他人在无头模式下遇到警报问题吗?

Anyone else having this issue with alerts in headless mode?

  • chromedriver v.2.30.477691
  • Chrome版本59.0.3071.115

推荐答案

在Chrome 61上仍然存在此问题,因此我花了一段时间寻找其他解决方案.我最喜欢的是它的简单性,它是在显示警报之前注入javascript,以便自动接受警报.

Still having this problem as of Chrome 61, so I spent a while looking for a different solution. My favorite because of it's simplicity is injecting javascript before the alert is shown in order to automatically accept the alert.

只需将以下代码行 置于导致显示警报的行之前:

Just put the following line of code before the line that causes the alert to be shown:

driver.ExecuteJavaScript("window.confirm = function(){return true;}");

可与无头chrome和PhantomJS一起使用.

Works with both headless chrome and PhantomJS.

这篇关于chromedriver无头警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆