Selenium没有使用我自己设置的Chrome驱动程序,而是使用默认的驱动程序

2guxujil  于 2023-09-28  发布在  Go
关注(0)|答案(2)|浏览(143)

所以我试图用Python获取网页的源代码,出于兼容性的原因,我必须使用Google Chrome 114而不是最新的116。我使用一个服务来创建它,并下载了我自己的版本,* 应该 * 工作,但它似乎只是完全忽略它,并使用我的系统之一。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

chrome_service = Service("/Users/*/Downloads/chromedriver_mac64/chromedriver") # >:(

driver = webdriver.Chrome(service=chrome_service)

url = "https://google.com"

driver.get(url) 

driver.implicitly_wait(10)

website_source = driver.page_source
print(website_source)

driver.quit()

输出量:

Traceback (most recent call last):
  File "/Users/*/Desktop/main.py", line 17, in <module>
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 84, in __init__
    super().__init__(
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
    super().__init__(
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 114
Current browser version is 116.0.5845.140 with binary path /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Stacktrace:
0   chromedriver                        0x000000010f7436b8 chromedriver + 4937400
1   chromedriver                        0x000000010f73ab73 chromedriver + 4901747
2   chromedriver                        0x000000010f2f8616 chromedriver + 435734
3   chromedriver                        0x000000010f32ad10 chromedriver + 642320
4   chromedriver                        0x000000010f32618a chromedriver + 622986
5   chromedriver                        0x000000010f32267c chromedriver + 607868
6   chromedriver                        0x000000010f369a08 chromedriver + 899592
7   chromedriver                        0x000000010f368ebf chromedriver + 896703
8   chromedriver                        0x000000010f35fde3 chromedriver + 859619
9   chromedriver                        0x000000010f32dd7f chromedriver + 654719
10  chromedriver                        0x000000010f32f0de chromedriver + 659678
11  chromedriver                        0x000000010f6ff2ad chromedriver + 4657837
12  chromedriver                        0x000000010f704130 chromedriver + 4677936
13  chromedriver                        0x000000010f70adef chromedriver + 4705775
14  chromedriver                        0x000000010f70505a chromedriver + 4681818
15  chromedriver                        0x000000010f6d792c chromedriver + 4495660
16  chromedriver                        0x000000010f722838 chromedriver + 4802616
17  chromedriver                        0x000000010f7229b7 chromedriver + 4802999
18  chromedriver                        0x000000010f73399f chromedriver + 4872607
19  libsystem_pthread.dylib             0x00007ff8185474e1 _pthread_start + 125
20  libsystem_pthread.dylib             0x00007ff818542f6b thread_start + 15
5fjcxozz

5fjcxozz1#

您可以使用https://github.com/seleniumbase/SeleniumBase将任何Chrome浏览器版本与任何chromedriver版本混合使用。
pip install seleniumbase之后,您可以使用python运行以下脚本,以强制已安装的Chrome版本使用特定的chromedriver版本:

from seleniumbase import Driver

driver = Driver(browser="chrome", driver_version="114")
driver.get("https://google.com")
driver.quit()

以下是第一次运行时的输出,如果你的PATH上还没有chromedriver 114:

Warning: chromedriver update needed. Getting it now:

*** chromedriver to download = 114.0.5735.90 (Legacy Version)

Downloading chromedriver_mac_arm64.zip from:
https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_mac_arm64.zip ...
Download Complete!

Extracting ['chromedriver'] from chromedriver_mac_arm64.zip ...
Unzip Complete!

The file [chromedriver] was saved to:
~/seleniumbase/drivers/chromedriver

Making [chromedriver 114.0.5735.90] executable ...
[chromedriver 114.0.5735.90] is now ready for use!
bq9c1y66

bq9c1y662#

不知道你用的是什么版本的 selenium 。如果您使用4.11.0或更高版本,您可以使用以下代码启动CFT version of chrome。不需要下载和设置chromedriver.exechrome.exe的路径,selenium会处理所有这些。

验证码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.browser_version = 114

driver = webdriver.Chrome(options=options)
url = "https://google.com"
driver.get(url)
driver.implicitly_wait(10)
website_source = driver.page_source
print(website_source)

**注意:**Selenium将下载并使用Chrome for Testing版本的Chrome浏览器,而不是普通的Chrome浏览器。当你运行上面的代码时,你会看到浏览器及其版本如下:

相关问题