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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With