|
17 | 17 | from botcity.base.utils import only_if_element |
18 | 18 | from bs4 import BeautifulSoup |
19 | 19 | from PIL import Image |
20 | | -from selenium.common.exceptions import InvalidSessionIdException |
| 20 | +from selenium.common.exceptions import InvalidSessionIdException, WebDriverException |
21 | 21 | from selenium.webdriver.common.action_chains import ActionChains |
22 | 22 | from selenium.common.exceptions import StaleElementReferenceException |
23 | 23 | from selenium.webdriver.common.by import By |
@@ -260,10 +260,32 @@ def check_driver(): |
260 | 260 | self.capabilities = cap |
261 | 261 | driver_path = self.driver_path or check_driver() |
262 | 262 | self.driver_path = driver_path |
263 | | - self._driver = driver_class(**self._get_parameters_to_driver()) |
| 263 | + self._driver = self._instantiate_driver(driver_class=driver_class, func_def_options=func_def_options) |
264 | 264 | self._others_configurations() |
265 | 265 | self.set_screen_resolution() |
266 | 266 |
|
| 267 | + def _instantiate_driver(self, driver_class, func_def_options): |
| 268 | + """ |
| 269 | + It is necessary to create this function because we isolated the instantiation of the driver, |
| 270 | + giving options to solve some bugs, mainly in undetected chrome. |
| 271 | + """ |
| 272 | + try: |
| 273 | + driver = driver_class(**self._get_parameters_to_driver()) |
| 274 | + except WebDriverException as error: |
| 275 | + # TODO: 'Undetected Chrome' fix error to upgrade version chrome. |
| 276 | + if 'This version of ChromeDriver only supports Chrome version' in error.msg: |
| 277 | + self.stop_browser() |
| 278 | + try: |
| 279 | + correct_version = int(error.msg.split('Current browser version is ')[1].split('.')[0]) |
| 280 | + except Exception: |
| 281 | + raise error |
| 282 | + self.options = func_def_options(self.headless, self._download_folder_path, None, |
| 283 | + self.page_load_strategy) |
| 284 | + driver = driver_class(**self._get_parameters_to_driver(), version_main=correct_version) |
| 285 | + else: |
| 286 | + raise error |
| 287 | + return driver |
| 288 | + |
267 | 289 | def _get_parameters_to_driver(self): |
268 | 290 | if self.browser == Browser.UNDETECTED_CHROME: |
269 | 291 | return {"options": self.options, |
|
0 commit comments