python - Chromedriver 无法识别选项
问题描述
我正在尝试在 PyCharm for MacOS 上运行 chromedriver:
options = Options()
options.headless = True
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=options)
但是代码不断抛出错误:
File "/filepath/file.py", line 28, in <module>
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=optionss)
TypeError: __init__() got an unexpected keyword argument 'options'
如何解决这个问题?我从 ChromeDriver网站下载了 81.0.4044.69 版本并将其放在文件路径中。
提前致谢!
解决方案
以无头模式启动 chrome 的示例示例。
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=r"path of chromedriver.exe",chrome_options=options)
driver.get('your url')
另一个解决方案:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("window-size=1400,800")
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=r"path ofchromedriver.exe",options=chrome_options)
driver.get("https://www.google.com")