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
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)