Python Selenium Firefox自定义配置文件保存

时间:2018-10-16 02:42:29

标签: python selenium selenium-webdriver selenium-firefoxdriver

我刚刚开始使用python学习硒


from selenium import webdriver

MY_PROFILE = "D:\\FIREFOX_PROFILE"

FFP = webdriver.FirefoxProfile(MY_PROFILE)

print(FFP.profile_dir)
# OUTPUT: C:\Users\ABC\AppData\Local\Temp\****\***
# But it should be OUTPUT: D:\FIREFOX_PROFILE

DRIVER = webdriver.Firefox(firefox_profile = FFP)

print(FFP.profile_dir)
# OUTPUT: C:\Users\ABC\AppData\Local\Temp\****\***
# But it should be OUTPUT: D:\FIREFOX_PROFILE

我想将个人资料保存在某个地方,以便以后使用。

我还尝试创建RUN-> firefox.exe -p并创建一个新的个人资料(我无法使用创建的个人资料)。什么都没有。

我正在使用:

  • 硒版本:2.53.6
  • Python版本:3.4.4
  • Firefox版本:各种(49.0.2、45、38等)

我在Google中搜索过,但无法解决。有什么方法可以保存配置文件?

1 个答案:

答案 0 :(得分:0)

您需要使用python中的os模块帮助

import os

您可以获得文件和目录中描述的功能(如.getcwd())。 然后使用

p = webdriver.FirefoxProfile()
p.set_preference('browser.download.folderList', 2 )
p.set_preference('browser.download.manager.showWhenStarting', false)
p.set_preference('browser.download.dir', os.getcwd())
p.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls')
driver = webdriver.Firefox(p)

简而言之,您可以这样做

profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml")

可能是Setting selenium to use custom profile, but it keeps opening with default的副本