Java Selenium Web-Driver Firefox Profile - 禁用弹出窗口和警报窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20974729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Web-Driver Firefox Profile - Disable popup and alert windows
提问by barak manos
I am having a problem with certain websites that cause my browser to prompt an alert when I try to switch to a different URL, or even close the browser. Some examples:
我在某些网站上遇到问题,当我尝试切换到不同的 URL 或什至关闭浏览器时,这些网站会导致我的浏览器提示警报。一些例子:
In order to workaround the alert with Selenium, I need to switch to that alert, and then sometimes accept it and sometimes reject it (depending on the contents of the alert).
为了解决 Selenium 的警报,我需要切换到该警报,然后有时接受它,有时拒绝它(取决于警报的内容)。
I wish to avoid solving this problem that way because:
我希望避免以这种方式解决这个问题,因为:
I need to guess whether I should accept the alert or reject the alert.
Switching to the alert sometimes throws an exception, even thoughthe alert is present.
我需要猜测我是应该接受警报还是拒绝警报。
即使存在警报,切换到警报有时也会引发异常。
What preferences do I need to set in the Firefox-Profile, in order to prevent the browser from issuing such alerts (or any other alerts for that matter)?
我需要在 Firefox 配置文件中设置哪些首选项,以防止浏览器发出此类警报(或任何其他与此相关的警报)?
Answers in Java or Python will be highly appreciated.
Java 或 Python 中的答案将不胜感激。
Thanks
谢谢
回答by Steve Weaver Crawford
As far as I know it's not possible to disable native browser events like alerts, so you'll just have to handle them better.
据我所知,不可能禁用警报等本机浏览器事件,因此您只需要更好地处理它们。
1) You should be able to use alert.getText()
to make an informed decision on whether to accept or dismiss an alert.
1) 您应该能够alert.getText()
就是否接受或解除警报做出明智的决定。
:
:
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
catch (Exception e) {
}
2) Use a WebDriverWait to avoid race conditions. See above
2) 使用 WebDriverWait 来避免竞争条件。看上面
回答by aish
I don't think Firefox profile would feature disabling such specific elements, but you can hard-code some lines of static logic that would remain consistent across the test case/project.
我认为 Firefox 配置文件不会禁用此类特定元素,但您可以硬编码一些静态逻辑行,以在整个测试用例/项目中保持一致。
Like Clickon the main page automatically closes the pop-up frame/alert msg on grooveshark.com
喜欢点击主页会自动关闭grooveshark.com上的弹出框/警报消息
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/#!/genre/Rap/1748"); //complete URL becomes http://grooveshark.com/#!/genre/Rap/1748
driver.findElement(By.linkText("more…")).click(); // clicks a hyper-link which opens up that frame/pop-up
driver.findElement(By.id("lightbox-outer")).click(); // clicks outside the opened-up frame, or simply clicks on the main page in background
}
lightbox-outeris the main page.
lightbox-outer是主页。
回答by java_geek
To my knowledge you can only disable that behaviour globally. There is a preference called dom.disable_beforeunload. You should change its value to true. With Selenium, you can create a new customized Firefox profile:
据我所知,您只能全局禁用该行为。有一个名为dom.disable_beforeunload的首选项。您应该将其值更改为 true。使用 Selenium,您可以创建一个新的自定义 Firefox 配置文件:
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("dom.disable_beforeunload", true);
FirefoxDriver driver = new FirefoxDriver(customProfile);
回答by K. Dumre
You can't disable the popups (alert), just do alert.accept() means clicking the ok button of alert modal or alert.dismiss() means clicking the cancel or close button.
您不能禁用弹出窗口(警报),只需执行 alert.accept() 表示单击警报模式的 ok 按钮或 alert.dismiss() 表示单击取消或关闭按钮。
The worst in this case is that you need to wait a certain time if you are not very sure that alert is going to be present or not.
在这种情况下,最糟糕的是,如果您不太确定是否会出现警报,则需要等待一段时间。
If the alert is present as a result of event sucess( just like clicing the button, webpage asks for your confirmation ) you dont need to do wait wait.until(ExpectedConditions.alertIsPresent());
you can save the time by going to next step which is switching the drver to alert. i.e
如果由于事件成功而出现警报(就像点击按钮一样,网页会要求您确认),您无需等待, wait.until(ExpectedConditions.alertIsPresent());
您可以通过转到下一步将驱动程序切换到警报来节省时间。IE
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
just to be sure you can use a small waiting time, but yes it depends uponthe network speed to load the web page for the case that alert is present when webpage is loaded.
只是为了确保您可以使用一小段等待时间,但是,对于加载网页时出现警报的情况,这取决于加载网页的网络速度。