1717from botcity .base .utils import only_if_element
1818from bs4 import BeautifulSoup
1919from PIL import Image
20- from selenium .common .exceptions import InvalidSessionIdException
20+ from selenium .common .exceptions import InvalidSessionIdException , WebDriverException
2121from selenium .webdriver .common .action_chains import ActionChains
2222from selenium .common .exceptions import StaleElementReferenceException
2323from selenium .webdriver .common .by import By
2424from selenium .webdriver .common .keys import Keys
2525from selenium .webdriver .remote .webelement import WebElement
2626from selenium .webdriver .support .wait import WebDriverWait , TimeoutException , NoSuchElementException
2727from selenium .webdriver .support import expected_conditions as EC
28+ from selenium .webdriver .common .print_page_options import PrintOptions
2829
2930from . import config , cv2find , compat
3031from .browsers import BROWSER_CONFIGS , Browser , PageLoadStrategy
@@ -259,13 +260,35 @@ def check_driver():
259260 self .capabilities = cap
260261 driver_path = self .driver_path or check_driver ()
261262 self .driver_path = driver_path
262- 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 )
263264 self ._others_configurations ()
264265 self .set_screen_resolution ()
265266
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+
266289 def _get_parameters_to_driver (self ):
267290 if self .browser == Browser .UNDETECTED_CHROME :
268- return {"driver_executable_path" : self . driver_path , " options" : self .options ,
291+ return {"options" : self .options ,
269292 "desired_capabilities" : self .capabilities }
270293 if compat .version_selenium_is_larger_than_four ():
271294 return {"options" : self .options , "service" : self ._get_service ()}
@@ -280,7 +303,7 @@ def _get_service(self):
280303 return service
281304
282305 def _others_configurations (self ):
283- if self .browser == Browser .UNDETECTED_CHROME :
306+ if self .browser in [ Browser .UNDETECTED_CHROME , Browser . CHROME , Browser . EDGE ] :
284307 """
285308 There is a problem in undetected chrome that prevents downloading files even passing
286309 download_folder_path in preferences.
@@ -1131,15 +1154,13 @@ def print_pdf(self, path=None, print_options=None):
11311154 return default_path
11321155
11331156 if print_options is None :
1134- print_options = {
1135- 'landscape' : False ,
1136- 'displayHeaderFooter' : False ,
1137- 'printBackground' : True ,
1138- 'preferCSSPageSize' : True ,
1139- 'marginTop' : 0 ,
1140- 'marginBottom' : 0
1141- }
1142- data = self ._webdriver_command ("print" , print_options )
1157+ print_options = PrintOptions ()
1158+ print_options .page_ranges = ['1-2' ]
1159+ print_options .margin_top = 0
1160+ print_options .margin_bottom = 0
1161+ print_options .background = True
1162+ print_options .orientation = 'landscape'
1163+ data = self ._driver .print_page (print_options )
11431164 bytes_file = base64 .b64decode (data )
11441165 if not path :
11451166 path = default_path
0 commit comments