无法单击 webdriver 中的警报

Not able to click alerts in webdriver

我想关闭警报。我尝试了以下两种方法,但没有任何效果。

有人可以解释一下这是怎么回事吗?

我使用的环境是...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)

这不是警告框,它是一种 Selenium Alert class 无法处理的浏览器通知警告。可能有两种处理方法...

您可以使用 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);