我試圖使用無頭谷歌瀏覽器在 macOS Big Sur 中抓取一些網站內容,這是我的 python 3 代碼:
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)
當我運行此代碼時,顯示錯誤:
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
我該怎么做才能讓它發揮作用?
uj5u.com熱心網友回復:
代替
opts = Options()
opts.set_headless()
請用
options = ChromeOptions()
options.add_argument("--headless")
或者
options = Options()
options.add_argument("--headless")
進口:
from selenium.webdriver.chrome.options import Options as ChromeOptions
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/367177.html
上一篇:硒觸發點擊事件時無法定位元素
