首页 > 解决方案 > Go-selenium:无法建立连接,因为目标机器主动拒绝它

问题描述

我尝试去windows防火墙并打开端口4444,但这仍然不起作用。这是我得到的完整错误:

Failed to open session: Post http://localhost:4444/wd/hub/session: dial tcp [::1]:4444: connectex: No connection could be made because the target machine actively refused it.

最后这是我正在使用的包:“sourcegraph.com/sourcegraph/go-selenium”。我从来没有在 python 中遇到过这个问题,但我认为整个应用程序都被允许通过防火墙,所以如果我能做到这一点,那就太好了。

编辑:我的所有代码(从 gi​​thub 示例中窃取)

package main

import (
    "sourcegraph.com/sourcegraph/go-selenium"
    "fmt"
)

func ExampleFindElement() {
    var webDriver selenium.WebDriver
    var err error
    caps := selenium.Capabilities(map[string]interface{}{"browserName": "firefox"})
    if webDriver, err = selenium.NewRemote(caps, "http://localhost:4444/wd/hub"); err != nil {
        fmt.Printf("Failed to open session: %s\n", err)
        return
    }
    defer webDriver.Quit()

err = webDriver.Get("https://sourcegraph.com/sourcegraph/go-selenium")
if err != nil {
    fmt.Printf("Failed to load page: %s\n", err)
    return
}

if title, err := webDriver.Title(); err == nil {
    fmt.Printf("Page title: %s\n", title)
} else {
    fmt.Printf("Failed to get page title: %s", err)
    return
}

var elem selenium.WebElement
elem, err = webDriver.FindElement(selenium.ByCSSSelector, ".repo .name")
if err != nil {
    fmt.Printf("Failed to find element: %s\n", err)
    return
}

if text, err := elem.Text(); err == nil {
    fmt.Printf("Repository: %s\n", text)
} else {
    fmt.Printf("Failed to get text of element: %s\n", err)
    return
}

// output:
// Page title: go-selenium - Sourcegraph
// Repository: go-selenium
}

func main() {
    ExampleFindElement()
}

标签: seleniumgo

解决方案


推荐阅读