Many users using selenium will encounter a problem. Our chromedriver is often incompatible with the local Chrome browser version, resulting in the failure of instantiating webdriver.Chrome. The error information is as follows:
Small climb in the previous blog( Compatibility between chrome and chromedriver versions of python selenium automation ), it encapsulates the method to obtain the chrome version and the chromedriver version respectively. When the major version numbers of the two versions are different, they are automatically networked in the Taobao source warehouse: ChromeDriver Mirror (taobao.org) Download the corresponding version of chromedriver in to realize the automatic update of chromedriver based on requirements, so that the two can match for a long time.
In fact, after some exploration, Xiaobo found that there was a third-party professional python library webdriver_manager gracefully solves this problem, and its background update chromedriver uses Google's official chromedriver warehouse. This URL( http://chromedriver.storage.googleapis.com/index.html )It can be accessed normally in China. The biggest advantage over Taobao source warehouse is that it always has the latest version of chrome driver, and the warehouse is updated faster, as shown in the figure below:
In fact, the webdriver manager Library (installed via PIP install webdriver manager) can also be used to update edgecorniumdriver, GeckoDriver, IEDriver and OperaDriver files. How to do this? In fact, we only need two or three lines of code when executing:
driver_path=ChromeDriverManager().install()
driver = webdriver.Chrome(executable_path=driver_path)
Of course, if we have more exploration spirit, we might as well take a look at its source code to understand its essence. The source code of ChromeDriverManager class is as follows:
class ChromeDriverManager(DriverManager): def __init__(self, version="latest", os_type=utils.os_type(), path=None, name="chromedriver", url="https://chromedriver.storage.googleapis.com", latest_release_url="https://chromedriver.storage.googleapis.com/LATEST_RELEASE", chrome_type=ChromeType.GOOGLE, log_level=logging.INFO, print_first_line=True, cache_valid_range=1): super().__init__(path, log_level=log_level, print_first_line=print_first_line, cache_valid_range=cache_valid_range) self.driver = ChromeDriver(name=name, version=version, os_type=os_type, url=url, latest_release_url=latest_release_url, chrome_type=chrome_type) def install(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:
class ChromeDriver(Driver): def __init__(self, name, version, os_type, url, latest_release_url, chrome_type=ChromeType.GOOGLE): super(ChromeDriver, self).__init__(name, version, os_type, url, latest_release_url) self.chrome_type = chrome_type self.browser_version = get_browser_version_from_os(chrome_type) def get_os_type(self): if "win" in super().get_os_type(): return "win32" return super().get_os_type() def get_latest_release_version(self): log(f"Get LATEST driver version for {self.browser_version}") resp = requests.get(f"{self._latest_release_url}_{self.browser_version}") validate_response(resp) return resp.text.rstrip()
This method will call the find_driver method of DriverCache class by default:
def find_driver(self, browser_version, driver_name, os_type, driver_version): metadata = self.get_metadata() key = f"{os_type}_{driver_name}_{driver_version}_for_{browser_version}" if key not in metadata: log(f"There is no [{os_type}] {driver_name} for browser {browser_version} in cache") return None driver_info = metadata[key] if not self.__is_valid(driver_info): return None path = driver_info['binary_path'] log(f"Driver [{path}] found in cache") return path
If the system can find the driver of the corresponding version in a specific path, it can call it directly. Otherwise, call the get_latest_release_version method to get the latest driver online and put it in the corresponding system path. For example, the latest version of chromedriver on my computer is uniformly stored in the path shown in the figure below for reference:
Due to the automatic upgrade strategy of computer-side Chrome browser, the local chrome river is always not compatible in time. If you have such problems, don't you try it quickly?