Reputation: 193
So recently I was looking into trying to get a headless browser for Selenium for web scraping and I came upon this solution on multiple websites
from selenium import webdriver
geckodriver = 'C:UsersgraysonDownloadsgeckodriver.exe'
headOption = webdriver.FirefoxOptions()
HeadOption.setheadless()
browser = webdriver.Firefox(executable_path=geckodriver, firefox_options=headOption)
browser.get('https://www.duckduckgo.com')
browser.save_screenshot('C:UsersgraysonDownloadsheadless_firefox_test.png')
browser.quit()
However I was still getting errors while trying to use the properties and arguments in this code.From what I can tell they seem to be outdated. What is the way to solve this problem?
Upvotes: 1
Views: 6313
Reputation: 193
from selenium import webdriver
geckodriver = 'C:UsersgraysonDownloadsgeckodriver.exe'
headOption = webdriver.FirefoxOptions()
headOption.**add_argument('-headless')**
browser = webdriver.Firefox(executable_path=geckodriver, **options**=headOption)
browser.get('https://www.duckduckgo.com')
browser.save_screenshot('C:UsersgraysonDownloadsheadless_firefox_test.png')
browser.quit()
The asterisk mark the differences. Basically Selenium doesn't like the '.setheadless' property anymore and has replaced the "firefox_options" argument with just "options"
I hope this was helpful
Upvotes: 3
Reputation: 19989
Don't copy paste code from the net with out understanding what it does.
firefox option doesn't have any method like setHeadless, but has a property called headless
headOption = webdriver.FirefoxOptions()
headOption.headless = True
driver = webdriver.Firefox(options=headOption)
you can set headless like this or by passing argument
headOption = webdriver.FirefoxOptions()
headOption.add_argument("--headless")
driver = webdriver.Firefox(options=headOption)
Both does the same thing , headless property pass the argument --headless under the hood
Upvotes: 4