Мне нужно использовать селен в Golang, я использую пример из репозитория https://github.com/tebeka/selenium Каталог "vender" с файлом Selenium и файлом драйвера был добавлен в каталог root проект.
Мой код:
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
В чем дело ? Как найти причину ошибки?