2

So I've been trying to get headless chrome working for days. I have no idea whats wrong !! I've tried everything I can find in the forums relating to the issue.

Right now this is the code im running (it's a direct snippet from someone else's tutorial which works fine for them):

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


browser_name = "chrome"
if browser_name == 'chrome':
    options = webdriver.ChromeOptions()
    options.headless = True
    driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
    start_url = "https://google.com"
    driver.get(start_url)
    print(driver.page_source.encode("utf-8"))
    driver.quit()

When I run that code I receive the following error

driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 64, in __init__
    desired_capabilities = options.self.to_capabilities()
AttributeError: 'Options' object has no attribute 'self'

It might be worth knowing that the chromedriver is in the correct path, I know this because when I run:

 browser_name = "chrome"

 if browser_name == 'chrome':
    
    driver = webdriver.Chrome(r"./chromedriver")
    start_url = "https://google.com"
    driver.get(start_url)
    print(driver.page_source.encode("utf-8"))
    driver.quit()

This works fine

2 Answers 2

1

There are two distinct approaches. If you are using:

options = webdriver.ChromeOptions()

Using only:

from selenium import webdriver

is sufficient.


But if you are using the following import:

from selenium.webdriver.chrome.options import Options

You have to use an instance of Options() to set the headless property as True as follows:

options = Options()
options.headless = True
driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Debanjan. This might have helped, however I did solve the issue ! I factory reset my computer and now the same code I posed above works fine. I opted for the nuclear approach lol
0

I had similar error when opening browser with Robot-Framework SeleniumLibrary:

robot.errors.HandlerExecutionFailed: AttributeError: 'Options' object has no attribute ''

Apparently the options argument, which is a string separated with ; should not end with a ; such as:

add_argument("--ignore-certificate-errors");add_argument("window-size=1920,1024");add_argument('--no-sandbox');add_argument('--disable-gpu');add_argument('--disable-dev-shm-usage');

Once I removed the last ; then the browser was launched with the required options.

Comments

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.