简体   繁体   English

硒-在无头浏览器中设置标头

[英]Selenium - set headers in headless browser

I'm trying to run a simple test on a headless browser. 我正在尝试在无头浏览器上运行一个简单的测试。 In my sample test when I take a screenshot I just get a blank screen when accessing my staging website, but production websites such as http://www.google.co.uk work correctly. 在我的示例测试中,当我截取屏幕截图时,访问登台网站时只会出现空白屏幕,但是生产网站(例如http://www.google.co.uk)可以正常工作。 So I guess we need to set headers in order to access our staging? 所以我想我们需要设置标题以便访问我们的暂存区吗?

Does anyone know how to do this on selenium for a headless browser? 有谁知道如何针对无头浏览器在硒上执行此操作?

The code I have is: 我的代码是:

public void testPhantom() throws IOException {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setJavascriptEnabled(true);
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "phantomjs");
    Webdriver driver = new PhantomJSDriver(caps);

    open("https://mywebsite.staging.com/");
    takeAScreenshot("Website");
}

================================================== ==================================================

UPDATE 更新

Found the solution by reading this post: PhantomJSDriver works for HTTP but not for HTTPS 通过阅读这篇文章找到了解决方案: PhantomJSDriver适用于HTTP但不适用于HTTPS

I have not tested this personally, but the GhostDriver documentation has the following at https://github.com/detro/ghostdriver#what-extra-webdriver-capabilities-ghostdriver-offers 我没有亲自测试过,但是GhostDriver文档在https://github.com/detro/ghostdriver#what-extra-webdriver-capabilities-ghostdriver-offers中具有以下内容

phantomjs.page.customHeaders.HEADER = VALUE - Add extra HTTP Headers when loading a URL phantomjs.page.customHeaders.HEADER = VALUE-加载URL时添加额外的HTTP标头

And from the PR: https://github.com/detro/ghostdriver/pull/229 : 从PR: https : //github.com/detro/ghostdriver/pull/229

PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX = "phantomjs.page.customHeaders."; //< notice the dot at the end
capabilities.setCapability(PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "it-IT");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用phantomjs无头浏览器处理selenium中的警报 - how to handle alerts in selenium with phantomjs headless browser 无法在 Java Selenium 无头浏览器中的新 window 中捕获文本 - Unable to capture text inside new window in Java Selenium Headless browser 如何使用Selenium Java中的机器人类在无头浏览器中上传文件 - How to upload file in headless browser using robot class in selenium java 如何运行无头的硒脚本,独立于主机和浏览器 - How to run headless selenium scripts, Host and Browser independent Play框架:在命令行上运行单硒测试(无头浏览器) - Play Framework: Run single selenium test on the command line (headless browser) Selenium Web驱动程序:findElement(By.name…和无头浏览器 - Selenium Web Driver: findElement(By.name … and headless browser 浏览器未缓存资源(已设置标题) - Browser not caching resources (headers set) 硒无头测试无头模式 - selenium headless test headless mode 是否需要在Linux机器上为无头firefox浏览器设置DISPLAY - Is it required to set the DISPLAY for headless firefox browser on linux machine 带有无头铬和硒的 NoSuchElementException - NoSuchElementException with headless chrome and selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM