我的chrome版本是127.0.6533.89 64位。我从[https://googlechromelabs.github.io/chrome-for-testing/#stable][1]下载了相应的chromedriver,版本为127.0.6533.88(我的是.89)。
当我运行程序时,出现以下错误:
Traceback (most recent call last):
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 95, in start
path = SeleniumManager().driver_location(browser)
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 74, in driver_location
result = self.run((binary, flag, browser))
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 93, in run
raise SeleniumManagerException(f"Selenium manager failed for: {command}.\n{stdout}{stderr}")
selenium.common.exceptions.SeleniumManagerException: Message: Selenium manager failed for: C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome.
WARN Error getting version of chromedriver 127. Retrying with chromedriver 126 (attempt 1/5)
WARN Error getting version of chromedriver 126. Retrying with chromedriver 125 (attempt 2/5)
WARN Error getting version of chromedriver 125. Retrying with chromedriver 124 (attempt 3/5)
WARN Error getting version of chromedriver 124. Retrying with chromedriver 123 (attempt 4/5)
WARN Error getting version of chromedriver 123. Retrying with chromedriver 122 (attempt 5/5)
ERROR The chromedriver version cannot be discovered
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\Kovy\Desktop\Programs\teumim.py", line 62, in <module>
first_name, last_name = get_webpage()
File "c:\Users\Kovy\Desktop\Programs\teumim.py", line 19, in get_webpage
driver = webdriver.Chrome(service=service, options=chrome_options)
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__
super().__init__(
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 101, in __init__
self.service.start()
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
raise err
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 88, in start
self._start_process(self.path)
File "C:\Users\Kovy\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 209, in _start_process
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
关于第二个问题,我将 chromedriver.exe 所在的文件添加到 PATH 中,但它仍然给我错误。关于第一个问题,我有最新的版本 - 怎么太旧了?
这是代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time
def get_webpage():
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument('--disable-dev-shm-usage')
# Specify the path to chromedriver.exe
chromedriver_path = 'C:/Users/Kovy/Download/chromedriver-win64/chromedriver-win64/chromedriver.exe'
service = Service(executable_path=chromedriver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
try:
email = "email"
password = "code"
url = 'https://accounts.google.com/'
driver.get(url)
time.sleep(15) # Wait for the page to load
input_username = driver.find_element(By.NAME, 'identifier')
input_username.send_keys(email)
input_username.send_keys(Keys.ENTER)
time.sleep(2)
input_password = driver.find_element(By.NAME, 'Passwd')
input_password.send_keys(password)
input_password.send_keys(Keys.ENTER)
time.sleep(25) # Wait for login to complete
webpage_content = driver.page_source
finally:
driver.quit()
# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(webpage_content, 'html.parser')
# Find all <p> tags and extract their text
paragraphs = soup.find_all('p')
text_content = '\n'.join([p.get_text() for p in paragraphs])
# Dummy values for first_name and last_name
first_name = "a"
last_name = "b"
return first_name, last_name
def download(first_name, last_name):
print(f"First Name: {first_name}, Last Name: {last_name}")
while True:
first_name, last_name = get_webpage()
download(first_name, last_name)
time.sleep(60) # Delay between iterations
尝试去掉“服务”并且不要使用可执行路径。