简体   繁体   English

Elixir & Hound 无头浏览器

[英]Elixir & Hound headless browser

Hound is pretty nice for testing web apps etc. when you need a headless browser.当您需要无头浏览器时, Hound非常适合测试 web 应用程序等。 I got it working, played around with the tests etc. but there are 2 questions about hound which maybe someone can explain who is familiar with Elixir:)我得到它的工作,玩弄测试等,但有 2 个关于猎犬的问题,也许有人可以解释谁熟悉 Elixir :)

1.) I'm using PhantomJS's remote WebDriver mode ( phantoms -w on localhost). 1.)我正在使用 PhantomJS 的远程 WebDriver 模式(本地主机上的phantoms -w )。 I've set 'config:hound, driver: "phantomjs"' in config.exs so a simple "navigate_to @url” launches a PhantomJS instance and works properly. Now I want change the HTTP User Agent String for this request. PhantomJS provides this page.settings hash. Running the request above against a local PhantomJS in remote WebDriver mode shows me the following settings:我在 config.exs 中设置了 'config:hound, driver: "phantomjs"' 所以一个简单的 "navigate_to @url" 启动了一个 PhantomJS 实例并正常工作。现在我想为此请求更改 HTTP 用户代理字符串。PhantomJS 提供此 page.settings hash. 在远程 WebDriver 模式下对本地 PhantomJS 运行上述请求会显示以下设置:

[INFO  - 2014-08-24T21:54:00.232Z] Session [27b92460-2bd9-11e4-a77f-1daa5df28587] - 
page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,
"loadImages":true,"localToRemoteUrlAccessEnabled":false,
"userAgent":"Mozilla/5.0 (Macintosh; PPC Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34","webSecurityEnabled":true}
[INFO  - 2014-08-24T21:54:00.232Z] Session [27b92460-2bd9-11e4-a77f-1daa5df28587] - page.customHeaders:  - {}
[INFO  - 2014-08-24T21:54:00.232Z] Session [27b92460-2bd9-11e4-a77f-1daa5df28587] - Session.negotiatedCapabilities -
{"browserName":"phantomjs","version":"1.9.7","driverName":"ghostdriver","driverVersion":"1.1.0",
"platform":"mac-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,
"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,
"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct”}}

The question is: how to change the userAgent above?问题是:上面的userAgent怎么改? I didn't find any example which deals with that.我没有找到任何处理这个问题的例子。 I know how it would look like running an PhantomJS instance directly as CLI tool with the appropriate JS config, but not sure, how hound manages that.我知道使用适当的 JS 配置直接将 PhantomJS 实例作为 CLI 工具运行会是什么样子,但不确定 hound 是如何管理它的。

2.) I also need to use HTTP proxies with authentication. 2.) 我还需要使用 HTTP 代理进行身份验证。 Same as in 1. I know how to deal with that launching PhantomJS from command line, but what is the right place to define them running on top of hound?与 1 相同。我知道如何处理从命令行启动 PhantomJS,但是定义它们在 hound 上运行的正确位置是什么?

You should pass a map as the additional_capabilities parameter to any function that starts a session. 您应该将map作为additional_capabilities参数传递给任何启动会话的函数。

Hound.start_session(%{userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"})

For the proxy option, the value should be another map with the properties. 对于代理选项,该值应该是具有属性的另一个映射。

Hound.start_session(%{proxy: %{property: "parameter", property: "parameter"}})

I never used it with proxy, so I'm not sure how to configure it properly. 我从未将它与代理一起使用,所以我不确定如何正确配置它。

I spent quite a bit of time trying to get this working. 我花了很多时间试图让这个工作。 The answer from cevado didn't work for me, but I was able to set the user-agent by setting the following. cevado的答案对我不起作用,但我可以通过设置以下内容来设置用户代理。

Hound.start_session(%{"phantomjs.page.settings.userAgent" => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"})

Hope this helps. 希望这可以帮助。

In addition:此外:

  • according to the documentation of current version -> {:hound, "~> 1.0"} , the atom: :user_agent must be used:根据当前版本的文档 -> {:hound, "~> 1.0"} ,必须使用 atom: :user_agent

     Hound.start_session(%{user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"})

Config in config/config.exs (selenium):config/config.exs (selenium) 中配置:

config :hound, driver: "chrome_driver", browser: "chrome_headless"

Example module for test purpose:用于测试目的的示例模块:

defmodule Example.Test do
  use Hound.Helpers

  def get_github_home() do
    Hound.start_session(%{user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"})
    navigate_to("https://www.github.com/")
    
    IO.inspect(page_source())
    
    Hound.end_session()
  end
end

Usage (iex):用法(iex):

Example.Test.get_github_home()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM