Не удается нажать на оповещения в веб-драйвере
Я хочу отклонить предупреждение. Я попробовал с ниже двумя методами ничего не работает.
Может кто-нибудь пролить свет на то, что не так?
Я использую среду...selenium-3.8.1, FF 53, Windows 7, драйвер геккона 16.
Также попробовал с selenium-3.8.1, chrome 64, Windows 7, драйвер chrome 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();
}
}
Пожалуйста, смотрите результаты ниже для каждого метода и изображения оповещения. Изображение оповещения
++++++++++++++++++++++++++++++++++++++++++++++++
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);
Другой способ вам нужно создать chrome preference map
с ChromeOptions
как показано ниже...
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);