简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM