指定Chrome可执行文件路径后丢失浏览器控制权问题求助
问题原因与解决方案
你混淆了参数的作用:Service的executable_path是用来指定ChromeDriver驱动程序的路径,而非Chrome浏览器可执行文件的路径。你把重命名后的rome.exe传给这个参数,相当于让Selenium把浏览器程序当成驱动来用,直接导致驱动与浏览器无法建立正常通信——这就是浏览器能启动但丢失控制权、driver.get()失效的原因。
正确代码示例
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # 指定自定义名称的Chrome浏览器路径 chrome_options = Options() chrome_options.binary_location = r"C:\Program Files\Google\Chrome\Application\rome.exe" # 若chromedriver不在系统默认路径,需指定其路径;否则可省略Service参数 driver_service = Service(executable_path=r"你实际的chromedriver路径\chromedriver.exe") # 同时传入Service和Options初始化驱动 driver = webdriver.Chrome(service=driver_service, options=chrome_options) driver.get(r"https://stackoverflow.com/") driver.quit()
关键说明
- 自定义Chrome浏览器名称时,必须通过
ChromeOptions的binary_location参数指定浏览器路径,而非通过Service传递。 - 默认的
webdriver.Chrome()能正常工作,是因为Selenium会自动检索系统中默认名称的Chrome浏览器及匹配的ChromeDriver。
内容的提问来源于stack exchange,提问作者purgatorian
相关产品推荐
相关产品推荐

