如何在Linux Python上设置Selenium Driver的Firefox配置文件

4

我希望能得到一点帮助,让Selenium使用我的Firefox配置文件。

我找到了我的Firefox配置文件的位置:/root/.mozilla/firefox/abcdefgh.default

import time
import random
import requests
from selenium import webdriver

profile = webdriver.FirefoxProfile()

with open("proxylist.txt") as f: 
     proxy_list = f.read().splitlines()
proxy_ip, proxy_port = random.choice(proxy_list).split(":")

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy_ip)
profile.set_preference("network.proxy.http_port", int(proxy_port))
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://www.ipleak.net")
time.sleep(60)
driver.close()

没有一些示例代码,就不可能知道你正在使用哪个库或者你是如何调用它的。 - memoselyk
@memoselyk,我已经添加了我的代码,如果你能帮忙,谢谢。 - James
@memoselyk 目前我根本没有调用它。我不确定该怎么做。现在这只是我的代码,没有任何错误。我想指定Selenium使用我的iceweasel浏览器,但唯一能让它工作的方法是使用Firefox驱动程序。我真的很想指定它使用我的真实浏览器。或者第二个选择是使用我的iceweasel或Firefox用户配置文件(因为我也下载了浏览器)。我的操作系统是Kali 2.0,我尝试使用我的Windows机器,但遇到了很多困难。 - James
2个回答

4

根据 webdriver.FirefoxProfile 的文档:

:args:

profile_directory:您想使用的配置文件目录。默认情况下为 None,当创建对象时会创建一个新目录。

因此,以下代码应该有效:

profile = webdriver.FirefoxProfile('/root/.mozilla/firefox/abcdefgh.default')
driver = webdriver.Firefox(profile)

哇,谢谢伙计,哈哈,有些简单的东西我要花很长时间才能弄明白。祝你好运和一切顺利。 - James
如果这个回答解决了你的问题,请接受这个答案。 - memoselyk

-1
import time
import random
import requests
from selenium import webdriver

profile = webdriver.FirefoxProfile()

with open("proxylist.txt") as f: 
     proxy_list = f.read().splitlines()
proxy_ip, proxy_port = random.choice(proxy_list).split(":")

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy_ip)
profile.set_preference("network.proxy.http_port", int(proxy_port))
profile.update_preferences()
driver = webdriver.Firefox(profile)
driver.get("https://www.ipleak.net")
time.sleep(60)
driver.close()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接