无法在 webdriver 中单击警报
Posted
技术标签:
【中文标题】无法在 webdriver 中单击警报【英文标题】:Not able to click alerts in webdriver 【发布时间】:2018-02-08 14:40:34 【问题描述】:我想解除警报。我尝试了以下两种方法,没有任何效果。
有人能解释一下这是怎么回事吗?
我使用的环境是...selenium-3.8.1, FF 53, Windows 7, gecko driver 16。
另外,我尝试使用 selenium-3.8.1、chrome 64、Windows 7、chrome driver win 32
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class RedbusAlert
public static void main(String[] args) throws InterruptedException
System.setProperty("webdriver.gecko.driver", "I:\\Selenium\\geckodriver-v0.16.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//Method 1
driver.get("https://www.redbus.in/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Alert alert=driver.switchTo().alert();
String alertMessage=alert.getText();
System.out.println(alertMessage);
alert.dismiss();
//Method 2
// driver.get("https://www.redbus.in/");
// WebDriverWait wait= new WebDriverWait(driver, 20);
// Alert alertMessage2=wait.until(ExpectedConditions.alertIsPresent());
// System.out.println("alert message"+alertMessage2.getText());
// alertMessage2.dismiss();
请查看以下每种方法的结果和警报图片 Alert Image
++++++++++++++++++++++++++++++++++++++++++++++++
Result for Method 1
++++++++++++++++++++++++++++++++++++++++++++++++
Feb 08, 2018 7:36:50 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Exception in thread "main" org.openqa.selenium.NoAlertPresentException: No modal dialog is currently open
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
System info: host: 'RAVIKUMAR-PC', ip: '192.168.0.101', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities acceptInsecureCerts: true, browserName: firefox, browserVersion: 53.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:processID: 8816, moz:profile: C:\Users\RAVIKU~1\AppData\L..., pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.1, rotatable: false, specificationLevel: 0, timeouts: implicit: 0, pageLoad: 300000, script: 30000
Session ID: d06ed63a-3259-41fb-93f4-5ccc0f225893
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.alert(RemoteWebDriver.java:987)
at day17.RedbusAlert.main(RedbusAlert.java:27)
++++++++++++++++++++++++++++++++++++++++++++++++
Result for Method 2
++++++++++++++++++++++++++++++++++++++++++++++++
Feb 08, 2018 7:40:09 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:10 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:10 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
JavaScript warning: https://cdn-jp.gsecondscreen.com/static/tac.min.js, line 3: unreachable code after return statement
Feb 08, 2018 7:40:11 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:11 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:20 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:21 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:27 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:27 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:28 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:28 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:29 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Feb 08, 2018 7:40:29 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '400' -> incorrect JSON status mapping for 'no such alert' (404 expected)
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for alert to be present (tried for 20 second(s) with 500 MILLISECONDS interval)
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
System info: host: 'RAVIKUMAR-PC', ip: '192.168.0.101', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities acceptInsecureCerts: true, browserName: firefox, browserVersion: 53.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:processID: 7844, moz:profile: C:\Users\RAVIKU~1\AppData\L..., pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.1, rotatable: false, specificationLevel: 0, timeouts: implicit: 0, pageLoad: 300000, script: 30000
Session ID: 905dae2f-095e-4762-97b1-a688759cb366
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:82)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:231)
at day17.RedbusAlert.main(RedbusAlert.java:30)
【问题讨论】:
【参考方案1】:这不是警告框,它是Selenium Alert
类无法处理的浏览器通知警告。可能有两种处理方式...
您可以使用ChromeOptions
禁用它的一种方法
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(options);
您需要使用ChromeOptions
创建chrome preference map
的其他方式如下...
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications",1);
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
【讨论】:
感谢@Gaurav 的工作。请您回答以下2个问题。 1. 有没有办法在通知中选择“允许”、“阻止”等选项。 2.火狐浏览器禁用通知的代码是什么以上是关于无法在 webdriver 中单击警报的主要内容,如果未能解决你的问题,请参考以下文章
WebDriver在解除权限对话框警报后未检测到单击的底部元素
无法使用 java 在 firefox 上的 selenium webdriver 中处理警报
不存在警报/未找到模式对话框——WebDriver 无法捕获 JS 错误
如何处理 selenium webdriver 自动化的背靠背警报
尝试在 watir-webdriver/selenium-webdriver 脚本中按 CTRL+单击或 SHIFT+单击时出现“TypeError:无法将字符串转换为整数”