英文:
Selenium + Go - how to?
问题
我正在开始研究使用Go语言的Selenium,但是我没有找到太多的信息。
我正在使用github.com/tebeka/selenium。
在Python中,我只需要安装(pip install selenium)并编写以下代码来打开浏览器:
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')
driver.get('http://www.hp.com')
在Go语言中,我该如何做到同样的效果?
我尝试了以下代码,但它并没有像Python那样打开浏览器:
package main
import (
	"fmt"
	"github.com/tebeka/selenium"
)
func main() {
	selenium.ChromeDriver("./chromedriver.exe")
	caps := selenium.Capabilities{"browserName": "chrome"}
	selenium.NewRemote(caps, fmt.Sprintf("http://www.google.com", 80))
}
在Go语言中是否有一种简单的方法,就像Python的那3行代码一样,只是打开我的机器上的浏览器?
谢谢!
英文:
I'm starting to take a look on Selenium with Go language, but I didn't find too much info.
I'm using github.com/tebeka/selenium.
In Python, I just install (pip install selenium) and code like this to open a browser:
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')
driver.get('http://www.hp.com')
How do I do the same in go?
I'm trying this, but it does not open the browser like Python does:
package main
import (
	"fmt"
	"github.com/tebeka/selenium"
)
func main() {
	selenium.ChromeDriver("./chromedriver.exe")
	caps := selenium.Capabilities{"browserName": "chrome"}
	selenium.NewRemote(caps, fmt.Sprintf("http://www.google.com", 80))
}
Is there a simple way in go to just open the browser in my machine like that 3 Python lines do?
Thanks!
答案1
得分: 4
在Python的Selenium中,它会自动启动浏览器,而Golang则不会。你必须显式地运行浏览器(服务)。这是一个使用Golang和Chromedriver的简单示例。
package main
import (
	"github.com/tebeka/selenium"
    "github.com/tebeka/selenium/chrome"
)
func main() error {
	// 运行Chrome浏览器
	service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
	if err != nil {
		panic(err)
	}
	defer service.Stop()
	caps := selenium.Capabilities{}
	caps.AddChrome(chrome.Capabilities{Args: []string{
		"window-size=1920x1080",
		"--no-sandbox",
		"--disable-dev-shm-usage",
		"disable-gpu",
		// "--headless",  // 注释掉此行以查看浏览器
	}})
	driver, err := selenium.NewRemote(caps, "")
	if err != nil {
		panic(err)
	}
	driver.Get("https://www.google.com")
}
英文:
In python selenium, it automatically starts the browser while Golang doesn't. You have to run the browser (service) explicitly. This is a simple example of using Chromedriver with Golang.
package main
import (
	"github.com/tebeka/selenium"
    "github.com/tebeka/selenium/chrome"
)
func main() error {
	// Run Chrome browser
	service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
	if err != nil {
		panic(err)
	}
	defer service.Stop()
	caps := selenium.Capabilities{}
	caps.AddChrome(chrome.Capabilities{Args: []string{
		"window-size=1920x1080",
		"--no-sandbox",
		"--disable-dev-shm-usage",
		"disable-gpu",
		// "--headless",  // comment out this line to see the browser
	}})
	driver, err := selenium.NewRemote(caps, "")
	if err != nil {
		panic(err)
	}
	driver.Get("https://www.google.com")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论