目标
- 不获取错误(两个代码段生成相同的错误)
- 它在aws ec2 ubuntu上运行
- 导入此文件以使用python、selenium、beautifulsoup进行网页抓取,同时持续加载JS网页
- 我遵循这个指南L
我的代码
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
错误
[WDM] - ====== WebDriver manager ======
2022-07-11 15:49:01,250 INFO ====== WebDriver manager ======
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-18-0987a487086c> in <module>
2 from webdriver_manager.chrome import ChromeDriverManager
3
----> 4 driver = webdriver.Chrome(ChromeDriverManager().install())
~/.local/lib/python3.8/site-packages/webdriver_manager/chrome.py in install(self)
36
37 def install(self) -> str:
---> 38 driver_path = self._get_driver_path(self.driver)
39 os.chmod(driver_path, 0o755)
40 return driver_path
~/.local/lib/python3.8/site-packages/webdriver_manager/core/manager.py in _get_driver_path(self, driver)
27
28 def _get_driver_path(self, driver):
---> 29 binary_path = self.driver_cache.find_driver(driver)
30 if binary_path:
31 return binary_path
~/.local/lib/python3.8/site-packages/webdriver_manager/core/driver_cache.py in find_driver(self, driver)
93 os_type = driver.get_os_type()
94 driver_name = driver.get_name()
---> 95 driver_version = driver.get_version()
96 browser_version = driver.browser_version
97
~/.local/lib/python3.8/site-packages/webdriver_manager/core/driver.py in get_version(self)
41 def get_version(self):
42 self._version = (
---> 43 self.get_latest_release_version()
44 if self._version == "latest"
45 else self._version
~/.local/lib/python3.8/site-packages/webdriver_manager/drivers/chrome.py in get_latest_release_version(self)
35
36 def get_latest_release_version(self):
---> 37 self.browser_version = get_browser_version_from_os(self.chrome_type)
38 log(f"Get LATEST {self._name} version for {self.browser_version} {self.chrome_type}")
39 latest_release_url = (
~/.local/lib/python3.8/site-packages/webdriver_manager/core/utils.py in get_browser_version_from_os(browser_type)
150 return get_browser_version(browser_type, metadata)
151
--> 152 cmd_mapping = {
153 ChromeType.BRAVE: {
154 OSType.LINUX: linux_browser_apps_to_cmd(
KeyError: 'google-chrome'
试图通过解决
- 获取KeyError:';WINDOWSSDKDIR';在Windows上运行Chromium浏览器源代码时
- https://exerror.com/chromedrivermanager-install-is-giving-keyerror-google-chrome/
- https://github.com/wkeeling/selenium-wire/issues/295
这可能是Selenium 4错误?
试试这个代码看看它是否工作
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
Ubuntu 22.04 Chromium Browser安装链接
终端命令:
sudo apt update
sudo apt install chromium-browser
测试
chromium-browser
如何在Ubuntu 20.04&18.04链接
终端命令:
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list"
sudo apt -y update
sudo apt -y install google-chrome-stable
检查版本
google-chrome --version
wget https://chromedriver.storage.googleapis.com/94.0.4606.61/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver