【发布时间】: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