以下載 Linux 平臺 119 版本 Chromium 與其 Webdriver 爲例:
首先在 Chromium 版本發佈頁 找到想要下載版本的 Branch Base Position 比如 1204232。然後在 https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Linux_x64/ 中搜索 1204232。如果發現搜索結果爲空,不用怕,把最後一位去掉試試,這時就會搜到 1204234。也可以再刪掉一位,就會出來更多相近的結果。點進去後下載 chrome-linux.zip 和 chromedriver_linux64.zip 就可以了。
unzip chrome-linux.zip cd chrome-linux #查看 Chrome 版本 ./chrome --version #Chromium 119.0.6045.0
在 Selenium 中指定 Chrome、Webdriver 與 Profile 位置
import os import import datetime import undetected_chromedriver as uc MAX_RUNTIME_SECONDS = 240 # Check for and kill any Chrome processes that have been running for too long for proc in psutil.process_iter(): try: if "chrome" in proc.name().lower(): create_time = proc.create_time() elapsed_time = time.time() - create_time if elapsed_time > MAX_RUNTIME_SECONDS: proc.kill() print("killed" + proc.name()) except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass script_dir = os.path.dirname(os.path.realpath(__file__)) profile_dir = os.path.join(script_dir, "chrome_profile") path_to_chrome_binary = os.path.abspath( script_dir + "/../../" + "chrome-linux64/chrome" ) driver_executable_path = os.path.abspath( script_dir + "/../../" + "chromedriver-linux64/chromedriver" ) proxy = "socks5://127.0.0.1:8080" options = uc.ChromeOptions() options.add_argument("--disable-notifications") options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-save-password-bubble") # Limit CPU and memory usage options.add_argument("--disable-software-rasterizer") options.add_argument("--disable-extensions") # options.add_argument("--disable-webgl") options.add_argument(proxy) options.binary_location = path_to_chrome_binary # if os.getenv("DEBUG") != "True": # options.add_argument("headless=True") prefs = { "credentials_enable_service": False, "profile.password_manager_enabled": False, "profile.privacy_sandbox_prompt_enabled": False, # "profile.managed_default_content_settings.images": 1, } options.add_experimental_option("prefs", prefs) try: driver = uc.Chrome( executable_path=driver_executable_path, browser_executable_path=path_to_chrome_binary, options=options, version_main=119, user_data_dir=profile_dir, # use_subprocess=False, ) except Exception as e: return None driver.set_window_size(1366, 768) driver.set_page_load_timeout(30) #fix timeout bug try: driver.get(url) except Exception as e: try: #send Esc key to stop loading driver.find_element(By.XPATH, '//body').send_keys(Keys.ESCAPE) time.sleep(1) # check page loaded WebDriverWait(driver, 10).until( EC.visibility_of_element_located( (By.XPATH, "//dl[@class='dfn']") ) ) except Exception as e: #take a screen shot png_name = datetime.datetime.now().isoformat()[:19] driver.save_screenshot(png_name + "_load" + ".png") return None