selenium.webdriver.firefox.options - what is it about?
Solution 1
Options is a class in the selenium firefox webdriver package. opts is an instance of the Options class instantiated for the program.
When the code says:
opts = Options()
Python creates an instance of the class, and uses the the variable opts as the access point.
When the code says:
opts.set_headless()
Python is updating the instance of Options, to store the information “the user of this wants to start a headless instance of the browser”
When the code says:
browser = Firefox(options=opts)
Python is creating an instance of the Firefox class, and sending it the opts variable to configure the new instance. In this case, the only option that has been modified from the defaults is the headless flag.
Solution 2
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
#--| Setup
options = Options()
options.add_argument("--headless")
caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
browser = webdriver.Firefox(firefox_options=options, capabilities=caps, executable_path=r"geckodriver.exe")
#--| Parse
browser.get('https://duckduckgo.com')
logo = browser.find_elements_by_css_selector('#logo_homepage_link')
print(logo[0].text)
this code works ( gives output About DuckDuckGo ). I was told that opts.set_headless()
is deprecated, maybe that's why it didn't give me any result.
MiloshB
Updated on July 09, 2022Comments
-
MiloshB almost 2 years
I'm looking at this code:
#! python3 from selenium.webdriver import Firefox from selenium.webdriver.firefox.options import Options opts = Options() opts.set_headless() assert opts.headless # Operating in headless mode browser = Firefox(options=opts) browser.get('https://duckduckgo.com')
source: https://realpython.com/modern-web-automation-with-python-and-selenium/
the idea is to call a headless browser but I don't understand the logic behind this code. What is 'options', and what is 'Options'? What do they exactly do? what
options=opts
stands for?Now trying to run this code and the webpage duckduckgo won't open. Any idea why?
-
MiloshB over 5 yearsThank you. A great explanation! None of these can be found in documentation, that's why I needed someone to explain this to me - step by step.
-
MiloshB over 5 yearsDon Simon ( and anyone else ), can you see my edited post? From some reason this code doesn't work, it doesn't open web site.
-
Don Simon over 5 yearsYou might try using the url http://www.duckduckgo.com That way you aren’t worried about encryption issues, nor redirects. Are you getting an error message?
-
Vyacheslav Konovalov almost 5 years
set_headless
method is deprecated, useopts.headless = True
instead. selenium-python.readthedocs.io/api.html