3

I can't update my Firefox profile preferences. If I add options.update_preferences () I get an error. I get AttributeError: 'Options' object has no attribute 'update_preferences'

How can I solve?

P.S: I wrote this code, which maybe can be useful to the Stack Overflow community, because the Firefox connection with preferences that has been used for years, has now been deprecated, because firefox_profile has been replaced by the Options object and executable_path by the Service object

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
   
profile_path = '/home/xxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default'

options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference("network.proxy.socks_remote_dns", False)

options.update_preferences() #here

service = Service('/usr/bin/geckodriver')
driver = Firefox(service=service, options=options)
  
driver.get("https://www.google.com")
driver.quit()
Erling Olsen
  • 1
  • 4
  • 15
  • doesn't seem like that would be needed. Is there a problem with leaving it out of the code? – pcalkins Nov 23 '21 at 21:14
  • @pcalkins options.update_preferences is for making options.set_preference effective. without a preferences update, then the options.set_preference do not apply – Erling Olsen Nov 23 '21 at 21:17
  • I've never had to use it myself. (in Selenium 3 anyway...though I do a options.setProfile(profile) method in Java...) Are you using Selenium 4 here? – pcalkins Nov 23 '21 at 21:28
  • @pcalkins I did not understand your answer, sorry. However I use version 4.1.0. How can I update and apply those options? – Erling Olsen Nov 23 '21 at 21:36

1 Answers1

3

update_preferences() updates the FirefoxProfile.DEFAULT_PREFERENCES through key, value pairs. It in the FirefoxProfile() class which is now Deprecated.

Instead you have to use Options and your effective working code block will be:

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

profile_path = '/home/xxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default'

options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference("network.proxy.socks_remote_dns", False)
service = Service('/usr/bin/geckodriver')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
driver.quit()

PS: Note that when you use options.set_preference() you no more require update_preferences()


References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I get an error. I get the error driver = Firefox (service = service, firefox_profile = profile) NameError: name 'Firefox' is not defined. Also I notice that you have removed from selenium.webdriver.firefox.options import Options. I added it because using the classic Firefox code with Tor proxy that everyone uses and that has been running on the web for years, I got the error that says to use the new Service and Option modules, because FirefoxProfile is deprecated. The message was "firefox_profile has been deprecated, please pass in an Options object". – Erling Olsen Nov 23 '21 at 21:56
  • Have added `from selenium.webdriver import Firefox` later :) – undetected Selenium Nov 23 '21 at 21:57
  • I still get an error, the one I was talking about before: driver = Firefox (service = service, firefox_profile = profile) DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object. For this reason I was trying to write new code, i.e. the code in the question. It worked fine. The problem was only the update. Do you have any other ideas? Thanks :) – Erling Olsen Nov 23 '21 at 22:00
  • Checkout the updated answer. Works perfecto now. – undetected Selenium Nov 23 '21 at 22:03
  • @DebanjanBDo I no longer need update_preferences? Because? So if I no longer need it, my code was fine too. What is the difference? Thanks :) – Erling Olsen Nov 23 '21 at 22:09
  • I have compared and notice that they are the same – Erling Olsen Nov 23 '21 at 22:10
  • Have you read the footer _PS: Note that when you use options.set_preference() you no more require update_preferences()_ ??? – undetected Selenium Nov 23 '21 at 22:36
  • Yes why? Can you give me a reason? Are you sure? – Erling Olsen Nov 23 '21 at 22:44
  • @heovan1999 Let me know if [this discussion](https://stackoverflow.com/a/70088853/7429447) answers your question. – undetected Selenium Nov 23 '21 at 23:00
  • I see that it is your answer and that you are talking about update preferences. But is there a reason why updates are no longer used? I'm curious :). For years it has been used. What has changed now? Thanks. P.S: I voted your answer in the other question in the link and I voted it in this question as well :) – Erling Olsen Nov 23 '21 at 23:05
  • it's never been needed in the Java bindings... so I'm really not sure what Python was doing there. I would think each Selenium session would have it's own preferences, even if you are choosing an existing profile. (Session stuff is usually stored in a temporary folder... and then deleted on exit.... else you'd be changing that profile's settings from the Selenium session.) – pcalkins Nov 24 '21 at 17:41
  • I wish I could have provided more info with those changes but who is interested in what happens under the hood :) give me some time to reopen the _Selenium Chat Room_ we will discuss there. However you will find some more finer info in the [_Reference_](https://stackoverflow.com/questions/69571950/deprecationwarning-firefox-profile-has-been-deprecated-please-pass-in-an-optio/70088853#70088853) link. – undetected Selenium Nov 24 '21 at 17:49