【问题标题】:Golang Selenium package - connect to selenium server and headless chromeGolang Selenium 包 - 连接到 selenium 服务器和无头 chrome
【发布时间】:2017-10-16 05:14:16
【问题描述】:

我正在使用 Go selenium 包https://godoc.org/github.com/tebeka/selenium

我正在localhost:4444 上的 docker 容器内运行 headless chrome + selenium-server

服务器似乎很好,因为我可以通过 http://localhost:4444/wd/hub/static/resource/hub.html 访问 Web 控制台

但我正在尝试让“Hello world”示例与现有的 docker 容器一起使用。

这是 Selenium 驱动程序的 GoDocs 页面中的示例:

// Run some code on play.golang.org and display the result
package main

import (
    "fmt"
    "time"

    "github.com/tebeka/selenium"
)

var code = `
package main
import "fmt"

func main() {
    fmt.Println("Hello WebDriver!\n")
}
`

// Errors are ignored for brevity.

func main() {
    // Connect to the selenium server
    caps := selenium.Capabilities{"browserName": "firefox"}
    wd, err := selenium.NewRemote(caps, "http://127.0.0.1:4444")
    if err != nil {
        fmt.Println(err)
    }
    defer wd.Quit()

    // Get simple playground interface
    wd.Get("http://play.golang.org/?simple=1")

    // Enter code in textarea
    elem, _ := wd.FindElement(selenium.ByCSSSelector, "#code")
    elem.Clear()
    elem.SendKeys(code)

    // Click the run button
    btn, _ := wd.FindElement(selenium.ByCSSSelector, "#run")
    btn.Click()

    // Get the result
    div, _ := wd.FindElement(selenium.ByCSSSelector, "#output")

    output := ""
    // Wait for run to finish
    for {
        output, _ = div.Text()
        if output != "Waiting for remote server..." {
            break
        }
        time.Sleep(time.Millisecond * 100)
    }

    fmt.Printf("Got: %s\n", output)
}

我尝试将"browserName" 更改为"chrome",但出现此错误:

panic: got content type "text/html", expected "application/json"

goroutine 1 [running]:
main.main()
    /home/user01/Code/golang_src/golang_exercises/33_selenium/selenium.go:28 +0x457
exit status 2

我在 GoDoc selenium 文档中找不到任何关于 chrome 浏览器以及如何通过 selenium-server 连接到它的内容。

我会很感激任何关于这里可能出了什么问题的提示。

更新:

似乎删除URL地址并将其留空已经解决了连接问题:

wd, err := selenium.NewRemote(caps, "")

也就是说,我仍然对示例有疑问。主要是它似乎连接到 Go Playground 网站,获取了正确的元素,但是在发送输入 elem.SendKeys(code) 时,它没有正确发送并且文本框为空。导致 Playground 输出错误:

Got: can't load package: package main: 
tmp/sandbox573608783/main.go:1:1: expected 'package', found 'EOF'

Program exited.

【问题讨论】:

    标签: google-chrome selenium docker go


    【解决方案1】:

    尝试在无头模式下运行它:

    caps := selenium.Capabilities{"browserName": "chrome"}
    
    chromeCaps := chrome.Capabilities{
        Path:  "",
        Args: []string{
            "--headless", // <<<
            "--no-sandbox",
            "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7",
        },
    }
    caps.AddChrome(chromeCaps)
    
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://127.0.0.1:%d", port))
    

    【讨论】:

      【解决方案2】:

      我也在 Docker 中使用 selenium,可以这样运行:

      wd, err := selenium.NewRemote(caps, "http://127.0.0.1:4444/wd/hub")

      【讨论】:

        【解决方案3】:

        经过一些调试,我发现问题是由于我的 Docker 容器内部没有 X 服务器。

        当 selenium 包尝试发送输入时,它会生成以下错误消息:

        unknown error: unknown error: an X display is required for keycode conversions, consider using Xvfb
          (Session info: headless chrome=60.0.3095.5)
          (Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 4.4.0-77-generic x86_64) (WARNING: The server did not provide any stacktrace information)
        Command duration or timeout: 40 milliseconds
        Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
        System info: host: 'e3bf5382c62d', ip: '171.14.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-77-generic', java.version: '1.8.0_121'
        Driver info: org.openqa.selenium.chrome.ChromeDriver
        Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5), userDataDir=/tmp/.org.chromium.Chromium.mFhqlU}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3095.5, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
        Session ID: 681f9de5f91baeaaa3100cf297767a2d
        

        我还没有在 Docker 容器中安装 X 服务器,但我相信它将修复通过 selenium 向无头 chrome 实例发送输入时发生的错误。

        【讨论】:

        • 你能分享你的docker文件吗?
        猜你喜欢
        • 2023-03-19
        • 2016-12-16
        • 2010-11-13
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 2014-08-20
        • 1970-01-01
        • 2013-01-14
        相关资源
        最近更新 更多