如何配置undetected_chromedriver使用本地便携版Chrome
如何配置undetected_chromedriver使用本地便携版Chrome
嗨,我来帮你搞定这个问题!要让undetected_chromedriver指定调用你的便携版Chrome,只需要在Chrome选项里加上可执行文件的路径就好,具体修改很简单,看下面的步骤:
核心修改点
你需要给ChromeOptions添加binary_location参数,直接指向你便携版Chrome的chrome.exe路径。这个参数的作用就是告诉驱动程序:“别找系统默认的Chrome了,就用我指定的这个!”
修改后的完整代码
我已经把你的代码调整好了,关键修改处标了注释:
# Import the required libraries import undetected_chromedriver as uc from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from webdriver_manager.chrome import ChromeDriverManager import time # Define Chrome options options = uc.ChromeOptions() # 关键修改:指定便携版Chrome的可执行文件路径 options.binary_location = r"C:\temp\GoogleChromePortable64\App\Chrome-bin\chrome.exe" # Set headless to False to run in non-headless mode options.headless = False # Chrome driver:这里因为指定了Chrome路径,undetected_chromedriver会自动匹配对应版本的驱动 driver = uc.Chrome(use_subprocess=True, options=options) # Go to the desired URL driver.get("https://library.usask.ca/#gsc.tab=0") # Wait until the input field is visible wait = WebDriverWait(driver, 10) q_field = wait.until(EC.presence_of_element_located((By.ID, "primoQueryTemp"))) # Use JavaScript to focus the element driver.execute_script("arguments[0].focus();", q_field) # Initiate typing into the field q_field.send_keys("_") # Click the field to trigger any other events q_field.click() # Keep the browser open for observation time.sleep(566) # Close the driver driver.quit()
额外说明
- 路径前面加
r是为了避免转义字符的问题,保证路径能被程序正确识别 - 不需要额外手动下载WebDriver,
undetected_chromedriver会自动根据你指定的Chrome版本匹配对应的驱动文件,省去了版本不兼容的麻烦
这样运行代码后,程序就会启动你指定的便携版Chrome啦!
备注:内容来源于stack exchange,提问作者RobM
相关产品推荐
相关产品推荐

