Many users of Selenium will encounter a problem. Our Chromedriver is often incompatible with the local Chrome browser version, causing us to fail to instantiate Webdriver.Chrome. The error message is roughly as follows:
An exception occurs: SessionNotCreatedException
Message: session not created: This version of ChromeDriver only supports Chrome version 90 Current browser version is 95.0.4638.69 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
There is a third-party professional Python library webdriver_manager that elegantly solves this problem, and its background update of chromedriver uses the official Google chromedriver warehouse. This URL (http://chromedriver.storage.googleapis.com/index.html) can be accessed normally in China. Compared with the Taobao source warehouse, the biggest advantage is that it always has the latest version of chromedriver.
In fact, the webdriver-manager library (installed via pip install webdriver-manager) can also be used to update EdgeChromiumDriver, GeckoDriver, IEDriver, and OperaDriver files. How to do it specifically? In fact, we only need two or three lines of code when we execute it:
definstall(self): log(f"Current {self.driver.chrome_type} version is {self.driver.browser_version}", first_line=True) driver_path = self._get_driver_path(self.driver)
os.chmod(driver_path, 0o755) return driver_path
The source code of the chromedriver class is as follows:
key = f"{os_type}_{driver_name}_{driver_version}_for_{browser_version}" if key notin metadata: log(f"There is no [{os_type}] {driver_name} for browser {browser_version} in cache") returnNone
driver_info = metadata[key]
ifnot self.__is_valid(driver_info): returnNone
path = driver_info['binary_path'] log(f"Driver [{path}] found in cache") return path
If the system can find the corresponding version of the driver in a specific path, it can be called directly. Otherwise, the get_latest_release_version method is called to obtain the latest driver online and placed in the corresponding system path.