Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AttributeError: 'Options' object has no attribute 'set_headless'

I am tried to use a headless google chrome to scrapy some website content in macOS Big Sur, this is my python 3 code:

from webbrowser import Chrome
from dolphin.common.commonlogger import CommonLogger
from selenium.webdriver import chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys


class FetchMusic:

    def __init__(self):
        print("fetch...")


if __name__ == '__main__':
    opts = Options()
    opts.set_headless()
    assert opts.headless  # Operating in headless mode
    browser = Chrome(executable_path=r"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
                     options=opts)
    browser.implicitly_wait(3)
    browser.get('https://ca.finance.yahoo.com/quote/AMZN/profile?p=AMZN')
    results = browser.find_elements_by_xpath('//*[@id="quote-header-info"]/div[3]/div/div/span[1]')
    for result in results:
        print(result.text)

when I run this code, shows error:

Traceback (most recent call last):
  File "/Users/dolphin/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/212.5457.59/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/dolphin/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/212.5457.59/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/dolphin/source/reddwarf/backend/pydolphin/dolphin/biz/music/fetch.py", line 18, in <module>
    opts.set_headless()
AttributeError: 'Options' object has no attribute 'set_headless'
python-BaseException

Process finished with exit code 1

what should I do to make it work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of

opts = Options()
opts.set_headless()

please use

options = ChromeOptions()
options.add_argument("--headless")

or

options = Options()
options.add_argument("--headless")

Imports :

from selenium.webdriver.chrome.options import Options as ChromeOptions
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading