python - 在 vscode Jupyter Notebook 中使用 head 运行 Selenium
问题描述
我在vscode中新建了一个Jupyter Notebook,输入如下代码:
from time import sleep
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.facebook.com/')
sleep(5)
browser.close()
当我将其作为 .py 文件运行时,它运行良好。但是当我在 .ipynb 文件中作为一个单元运行时,我得到了错误:
WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(chrome not reachable)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
---------------------------------------------------------------------------
WebDriverException Traceback (most recent call last)
<ipython-input-2-9d5c2a8914c9> in <module>
2 # this is tested on Firefox or you can use "webdriver.Chrome()"
3 from selenium import webdriver
----> 4 browser = webdriver.Chrome()
5 browser.get('https://www.facebook.com/')
6 sleep(5)
~/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
74
75 try:
---> 76 RemoteWebDriver.__init__(
77 self,
78 command_executor=ChromeRemoteConnection(
~/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
155 warnings.warn("Please use FirefoxOptions to set browser profile",
156 DeprecationWarning, stacklevel=2)
--> 157 self.start_session(capabilities, browser_profile)
158 self._switch_to = SwitchTo(self)
159 self._mobile = Mobile(self)
~/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
250 parameters = {"capabilities": w3c_caps,
251 "desiredCapabilities": capabilities}
--> 252 response = self.execute(Command.NEW_SESSION, parameters)
253 if 'sessionId' not in response:
254 response = response['value']
~/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
~/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(chrome not reachable)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
如果我添加:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
那么这工作正常。但我真的不希望它现在无头运行,因为我希望看到我在编写代码时所做的更改。如何让它在 vscode + jupyter notebook 中使用附加的头部运行?
解决方案
推荐阅读
- java - reading jp2 file with java and Jdeli
- google-cloud-platform - 我可以使用 gcloud 命令调整服务帐户的权限并启用对 firebase 函数内存储桶的写入访问权限吗?
- buildroot - 从本地源构建带有快照的包
- llvm - llc 没有调试或 -view-dag-combine1-dags 选项
- java - 静态字段的 Spring Boot 自动装配 BeanCreationException
- python - pytest.raises(Exception) 不适用于烧瓶 app.post
- python - Python Tweepy 编码(utf-8)
- php - apache htaccess 将应用程序根目录重定向到不存在的子目录
- sql - Will Materialized view refresh fail if I alter base table to add column
- python - No body in answer of GET request