执行不适用于https网站的脚本 - chrome selenium webdriver

时间:2017-11-02 11:07:43

标签: google-chrome selenium selenium-webdriver selenium-chromedriver google-chrome-headless

我正在尝试执行javascript作为测试的一部分。

以下是我的Chrome驱动程序设置

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("test-type");
        options.addArguments("headless");
        options.addArguments("--disable-web-security");
        options.addArguments("--allow-running-insecure-content");
        options.addArguments("--allow-insecure-localhost");
        options.addArguments("--reduce-security-for-testing");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        driver = new ChromeDriver(capabilities);
        driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);
        driver.get("https://samplesite.com/");

我正在尝试使用Javascript执行器在网站上执行脚本。

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(code);

由于以下错误,这不起作用。

The page at 'https://samplesite.com/' was loaded over HTTPS, 
but requested an insecure XMLHttpRequest endpoint 'http://localhost:8080/myapi'.
This request has been blocked; the content must be served over HTTPS.

但是当我删除headless选项并在控制台中执行带有警告消息的可见浏览器时,同样有效。

有人可以指导我。

1 个答案:

答案 0 :(得分:0)

查看错误堆栈跟踪说明了一切:

The page at 'https://samplesite.com/' was loaded over HTTPS, 
but requested an insecure XMLHttpRequest endpoint 'http://localhost:8080/myapi'.
This request has been blocked; the content must be served over HTTPS.

网址https://samplesite.com/加载了HTTPS,但jquery post script正在尝试调用HTTP上的网页

  

<强> On an https webpage you can only make Jquery/AJAX Request to https webpage only

了解您是否在http中指定httpsJquery Request会很有趣。

您还需要确保由于您请求的数据不在同一个域中,提供数据的域必须允许使用CORS或JSONP。