Search code examples
pythonseleniumheadlessheadless-browser

Getting error while running firefox browser in headless mode using python 3


I am just trying to run this using headless browser i don't understand why it keeps throwing me the error even if i have provided argument. Here i have read that it requires argument to pass in options.add_argument() :- https://seleniumhq.github.io/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.options.html#module-selenium.webdriver.firefox.options

Error :- TypeError: add_argument() missing 1 required positional argument: 'argument'

from selenium import webdriver
from selenium.webdriver.firefox.options import Options


options = Options.add_argument('-headless')
browser = webdriver.Firefox(options)
browser.get('https://intoli.com/blog/email-spy/')
browser.implicitly_wait(50)
heading = browser.find_element_by_xpath('//*[@id="heading-breadcrumb"]/div/div/div/h1').text
print(heading)
browser.close()

Solution

  • You should create an object Options before calling the property on it. For more informations about how @property works, see this answer.

    # create a new object
    options = Options()
    # calling the property (setter)
    options.add_argument('-headless')
    

    Update : Furthermore, it seems that there are other problems with your code sample. If you want to provide only firefox_options, you should do it as a keyword argument, so you must provide it explicitly:

    browser = webdriver.Firefox(firefox_options=options)