使用Selenium连接ChromeDriver时触发WebDriverException错误求助
问题解决:WebDriverException无法连接到Chrome服务
核心错误原因
你传给Service对象的路径是Chrome浏览器的可执行文件(chrome.exe),但Service需要的是ChromeDriver的可执行文件路径,这是导致连接失败的直接原因。
修复步骤
下载匹配版本的ChromeDriver
确保ChromeDriver版本与你的Chrome浏览器版本完全一致(查看Chrome版本:设置→关于Chrome),下载对应版本的ChromeDriver。修正路径配置
将chrome_driver_path改为ChromeDriver的实际路径,比如:chrome_driver_path = r'C:\Users\Aved Debbarma\Downloads\chromedriver-win64\chromedriver.exe'修复代码中的其他问题
原代码存在两处错误:find_element_by_xpath是Selenium旧版API,建议使用新版的find_element(By.XPATH, ...),需要先导入from selenium.webdriver.common.by import Byall_button = driver.find_element(...).click()中,click()方法返回None,后续调用all_button.click()会触发AttributeError,应拆分两行或直接调用click()
修正后的完整代码示例:
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 website = 'https://www.adamchoi.co.uk/overs/detailed' # 替换为你的ChromeDriver实际路径 chrome_driver_path = r'C:\Users\Aved Debbarma\Downloads\chromedriver-win64\chromedriver.exe' service = Service(chrome_driver_path) options = Options() # 推荐使用新版无头模式参数 options.add_argument('--headless=new') driver = webdriver.Chrome(service=service, options=options) driver.get(website) # 点击"All matches"按钮 all_button = driver.find_element(By.XPATH, '//label[@analytics-event="All matches"]') all_button.click() matches = driver.find_elements(By.TAG_NAME, 'tr') # 记得最后关闭驱动 driver.quit()
内容的提问来源于stack exchange,提问作者Suhail Ashraf
相关产品推荐
相关产品推荐

