gpt4 book ai didi

selenium - 无法创建 selenium.NewRemote

转载 作者:行者123 更新时间:2023-12-01 22:25:46 25 4
gpt4 key购买 nike

我需要在 Golang 中使用 selenium,我使用存储库中的示例 https://github.com/tebeka/selenium
包含 Selenium 文件和驱动程序文件的“vendor ”目录已添加到项目的根目录中。

我的代码:

package main

import (
"fmt"
"os"
"strings"
"time"

"github.com/tebeka/selenium"
)

func main() {
// Start a Selenium WebDriver server instance (if one is not already
// running).
const (
// These paths will be different on your system.
seleniumPath = "vendor/selenium-server-standalone-3.141.59.jar"
geckoDriverPath = "vendor/geckodriver-v0.26.0-linux64/geckodriver"
port = 8180
)
opts := []selenium.ServiceOption{
selenium.StartFrameBuffer(), // Start an X frame buffer for the browser to run in.
selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
//selenium.ChromeDriver(chromeDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
selenium.Output(os.Stderr), // Output debug information to STDERR.
}
selenium.SetDebug(true)
service, err := selenium.NewSeleniumService(seleniumPath, port, opts...)
if err != nil {
panic(err) // panic is used only as an example and is not otherwise recommended.
}
defer service.Stop()

// Connect to the WebDriver instance running locally.
caps := selenium.Capabilities{"browserName": "firefox"}

wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil {
panic(err)
}
defer wd.Quit()

// Navigate to the simple playground interface.
if err := wd.Get("http://play.golang.org/?simple=1"); err != nil {
panic(err)
}

// Get a reference to the text box containing code.
elem, err := wd.FindElement(selenium.ByCSSSelector, "#code")
if err != nil {
panic(err)
}
// Remove the boilerplate code already in the text box.
if err := elem.Clear(); err != nil {
panic(err)
}

// Enter some new code in text box.
err = elem.SendKeys(`
package main
import "fmt"
func main() {
fmt.Println("Hello WebDriver!\n")
}
`)
if err != nil {
panic(err)
}

// Click the run button.
btn, err := wd.FindElement(selenium.ByCSSSelector, "#run")
if err != nil {
panic(err)
}
if err := btn.Click(); err != nil {
panic(err)
}

// Wait for the program to finish running and get the output.
outputDiv, err := wd.FindElement(selenium.ByCSSSelector, "#output")
if err != nil {
panic(err)
}

var output string
for {
output, err = outputDiv.Text()
if err != nil {
panic(err)
}
if output != "Waiting for remote server..." {
break
}
time.Sleep(time.Millisecond * 100)
}

fmt.Printf("%s", strings.Replace(output, "\n\n", "\n", -1))

// Example Output:
// Hello WebDriver!
//
// Program exited.
}


如果我坐在“geckoDriverPath =”vendor/geckodriver-v0.26.0-linux64/geckodriver“”:
我在“selenium.NewRemote(...”行):
...
{
"fileName": "Thread.java",
"methodName": "run",
"className": "java.lang.Thread",
"lineNumber": 834
}
],
"message": "invalid argument: can't kill an exited process\nBuild info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'\nSystem info: host: 'k8s-s1.me', ip: '192.168.88.10', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-72-generic', java.version: '11.0.6'\nDriver info: driver.version: unknown\nremote stacktrace: ",
"error": "unknown error"
},
"status": 13

如果我坐在 "geckoDriverPath = "vendor/geckodriver-v0.26.0-linux64""行,则输出:
...
{
"fileName": "Thread.java",
"methodName": "run",
"className": "java.lang.Thread",
"lineNumber": 834
}
],
"message": "Unable to create new service: GeckoDriverService\nBuild info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'\nSystem info: host: 'k8s-s1.me', ip: '192.168.88.10', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-72-generic', java.version: '11.0.6'\nDriver info: driver.version: unknown",
"error": "session not created"
},
"status": 33

怎么了?如何查找错误原因?

最佳答案

你安装的是什么版本的火狐?该错误可能是由于驱动程序和 Firefox 版本不匹配造成的。 geckodriver-v0.26.0-linux64/geckodriver您尝试使用的 Firefox 版本要求为 >= 60 .

您还可以查看发行说明:https://github.com/mozilla/geckodriver/releases查看已知问题中的任何内容是否适用于您。

关于selenium - 无法创建 selenium.NewRemote,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com