tebeka / selenium

Selenium/Webdriver client for Go
MIT License
2.51k stars 410 forks source link

Can't create NewRemote #188

Open IvanBiv opened 4 years ago

IvanBiv commented 4 years ago

My code:

package main

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

    //"h12.io/socks"

    "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"
        chromeDriverPath = "vendor/chromedriver_linux64"
        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"}
    //caps := selenium.Capabilities{"browserName": "chrome"}

    // chromeCaps := chrome.Capabilities{
    //  Path:            "/path/to/chrome-binary",
    //  ExcludeSwitches: []string{"enable-automation"},
    // }
    // caps.AddChrome(chromeCaps)

    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.
}

If I sat "geckoDriverPath = "vendor/geckodriver-v0.26.0-linux64/geckodriver"": I take at line "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

if I sat line "geckoDriverPath = "vendor/geckodriver-v0.26.0-linux64"", than output:

...
            {
                "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

What's the matter? How to find the cause of the error?

zonorion commented 3 years ago

same problem :(