hunter/pre_chrome_driver_com.go

47 lines
845 B
Go
Raw Permalink Normal View History

package hunter
import (
"fmt"
"log"
"github.com/Pallinder/go-randomdata"
"github.com/tebeka/selenium"
)
// PreChromeUrl Chrome的url预处理
type PreChromeUrl struct {
PreBaseDriverUrl
}
// Before 驱动的预处理
func (u *PreChromeUrl) Before(ctx *TaskContext) {
var err error
var service *selenium.Service
if u.service == nil {
2020-05-18 10:24:01 +00:00
for i := 0; i < 50; i++ {
if u.Port == 0 {
u.Port = randomdata.Number(10000, 50000)
}
service, err = selenium.NewChromeDriverService("chromedriver", u.Port)
if err != nil {
log.Println(i, err)
} else {
break
}
}
u.service = service
}
if u.driver == nil {
caps := selenium.Capabilities{"browserName": "chrome"}
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", u.Port))
if err != nil {
panic(err)
}
u.driver = wd
}
}