0

I have the following code:

options = Options()
options = options.set_headless( headless=True)

class Sel_Driver():

    def __init__(self):
        self.driver = webdriver.Firefox(firefox_options=options)

I can then use self.driver.get(url) as part of a method to open urls I feed in. This works - I can feed in and open the URLs, but they don't in headless mode.

(I initially defined the driver as self.driver = webdriver.Firefox(firefox_options=Options().set_headless(headless=True) - but that didn't work, so I tried it as above).

What am I missing? I don't understand why the driver is able to open pages, but the options aren't enabled.

2 Answers 2

5

Please try following code :

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options)
Sign up to request clarification or add additional context in comments.

1 Comment

In newer versions i'm pretty sure you can also do options.headless = True.
0

This will work for you for sure. Try it.Please specify the path of the driver. It is for chrome change it to firefox.

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Users\\Username\\Downloads\\chromedriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('https://www.facebook.com/')
jks = driver.find_element_by_id("email").get_attribute("class")
print(jks)
driver.quit()

1 Comment

Thanks dude. I'm sure this would work, it's very similar to what I had previously. However, I tried putting it in the form of a class/object so that I wasn't instantiating a new webdriver for each url I want to open. As per the accepted answer, options.add_argument("--headless") does indeed fix the issue. Welcome to Stack Overflow =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.