Python and selenium: how to change Firefox's profile multiple times

耗尽温柔 提交于 2021-01-29 06:23:24

问题


I set up Firefox's profile and launched the driver with that profile, after that, I want to know how to change some preferences on that profile multiple times, or how to change the profile after launching the driver? here is my code for setting up the profile:

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.helperApps.alwaysAsk.force', False)
profile.set_preference('browser.download.dir', 'D:\\Workspace\\Res')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/x-7z-compressed")


driver = webdriver.Firefox(profile, executable_path=r"C:\geckodriver\geckodriver.exe")

回答1:


As per the current implementation of Selenium once you configure the WebDriver instance with the required Options and Capabilities and initialize the Web Browser session to open a Browsing Context, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.

So, in-order to change the firefox User Profile you have to initiate a new WebDriver session.


Reference

Here is @JimEvans clear and concise comment (as of Oct 24 '13 at 13:02) related to proxy settings capability:

When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.


Outro

You can find a relevant detailed discussion in How to set selenium webdriver from headless mode to normal mode within the same session?



来源:https://stackoverflow.com/questions/62864667/python-and-selenium-how-to-change-firefoxs-profile-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!