【问题标题】:Execute script not working for https sites - chrome selenium webdriver执行脚本不适用于 https 站点 - chrome selenium webdriver
【发布时间】:2017-11-02 11:07:42
【问题描述】:

我正在尝试执行 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 选项并作为可见浏览器执行时,同样的方法在控制台中显示警告消息。

有人可以指导我吗?

【问题讨论】:

  • 你要执行的脚本是什么?
  • 脚本从页面中识别出 href 元素并将其发布到我在本地托管的应用程序中。

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


【解决方案1】:

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

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.

URL https://samplesite.com/ 加载了HTTPS,但jquery post script 试图通过HTTP 调用网页

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

如果您在Jquery Request 中指定http 或https,将会很有趣。

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

【讨论】:

  • 是的。我明白这个问题。正如我在问题中提到的。如果我不使用无头选项,这将执行得非常好。所以我正在寻找一种解决方案,以便它也可以在无头选项中工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多