Selenium+ubuntu+firefox找不到profile文件解决

Selenium+ubuntu+firefox找不到profile文件解决,第1张

使用的firefox为商店snap安装

Python:
1.在firefox地址栏中输入:about:profiles
2. 创建新配置文件,名称如selenium
3. 取消新配置作为默认配置
4. 将新配置本地目录/home/user/snap/firefox/common/.cache/mozilla/firefox/a1nwuiny.selenium指定给selenium。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("-profile")
options.add_argument("/home/user/snap/firefox/common/.cache/mozilla/firefox/a1nwuiny.selenium")
br=webdriver.Firefox(options=options)
br.get(“http://www.baidu.com/”)

如果使用原有的profile给selenium, 当firefox已打开时,会提示已经运行,需要新建profile.

Golang:
golang也需要额外指定profile.
按照https://firefox-source-docs.mozilla.org/testing/geckodriver/Profiles.html的说明,将profile添加到Capablities的Args参数中
原文:

by appending --profile /some/location to the args capability, which will instruct geckodriver to use the profile in-place;or by setting the profile capability to a Base64-encoded ZIP of the profile directory.
第二种方法没成功。
package main
import (
    "fmt"
    "github.com/tebeka/selenium"
    "github.com/tebeka/selenium/firefox"
    "net"
    "os"
    "time"
)
const    geckoDriverPath = "/usr/local/bin/geckodriver"
func pickUnusedPort()(int ,error){
    addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
    if err != nil {
        return 0, err
    }
    l, err := net.ListenTCP("tcp", addr)
    if err != nil {
        return 0, err
    }
    port := l.Addr().(*net.TCPAddr).Port
    if err := l.Close(); err != nil {
        return 0, err
    }
    return port, nil 
}
func main(){
    port, err := pickUnusedPort()
    fmt.Println("port: ", port)
    
    opts := []selenium.ServiceOption{
        selenium.GeckoDriver(geckoDriverPath),
        selenium.Output(os.Stderr),
    }
    selenium.SetDebug(false)
    
    service, err := selenium.NewGeckoDriverService(geckoDriverPath, port, opts...)
    if err != nil {
        panic(err)
    }
    defer service.Stop()
    fmt.Println("Here 1")
    firecap := firefox.Capabilities{}
    firecap.Args=append(firecap.Args, "--profile")
    firecap.Args=append(firecap.Args, "/home/user/snap/firefox/common/.cache/mozilla/firefox/a1nwuiny.selenium")
    caps := selenium.Capabilities{"browserName":"firefox"}
    caps.AddFirefox(firecap)
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d",port))
        if err != nil {
            panic(err)
     }
     defer wd.Quit()
	if err = wd.Get("https://golang.google.cn/play/"); err != nil {

		panic(err)

	}
	//延时等待网站自身更新
	time.Sleep(time.Second * 3)
	// Get a reference to the text box containing code.
	elem, err := wd.FindElement(selenium.ByCSSSelector, "#code")
	if err != nil {
	    panic(err)
    }
    //清除默认代码
     err = elem.SendKeys(selenium.ControlKey+"a")
    if err != nil {
        fmt.Println(err)
    }
    err = elem.SendKeys(selenium.DeleteKey)
    if err != nil {
        fmt.Println(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)
    }
    time.Sleep(time.Second * 2)
    //click run button
    btn, err := wd.FindElement(selenium.ByCSSSelector, "#run")
    if err != nil {
        panic(err)
    }
    if err := btn.Click(); err != nil {
        panic(err)
    }
    
    //必须延时
    time.Sleep(time.Second*2)
    //wait for the program to finish running and get the output
    outputDiv, err := wd.FindElement(selenium.ByXPATH, "/html/body/main/div/div[3]/pre/span[2]")
    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
        }
        fmt.Println(output)
        time.Sleep(time.Second * 1)
    }
    fmt.Println("waiting....")
    time.Sleep(time.Second * 5)
}     

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/994884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存