output load chrome driver exception by check config options.

master
CHUN YU YAO 2023-06-09 22:59:38 +08:00
parent 24b366ffdc
commit 0d32ef04d4
3 changed files with 37 additions and 9 deletions

View File

@ -357,10 +357,23 @@ def get_chrome_options(webdriver_path, adblock_plus_enable, browser="chrome", he
return chrome_options, caps return chrome_options, caps
def load_chromdriver_normal(webdriver_path, driver_type, adblock_plus_enable, headless): def load_chromdriver_normal(config_dict, driver_type):
show_debug_message = True # debug.
show_debug_message = False # online
if config_dict["advanced"]["verbose"]:
show_debug_message = True
driver = None driver = None
Root_Dir = get_app_root()
webdriver_path = os.path.join(Root_Dir, "webdriver")
adblock_plus_enable = config_dict["advanced"]["adblock_plus_enable"]
headless = config_dict["advanced"]["headless"]
chromedriver_path = get_chromedriver_path(webdriver_path) chromedriver_path = get_chromedriver_path(webdriver_path)
if not os.path.exists(chromedriver_path): if not os.path.exists(chromedriver_path):
print("Please download chromedriver and extract zip to webdriver folder from this url:") print("Please download chromedriver and extract zip to webdriver folder from this url:")
print("請下在面的網址下載與你chrome瀏覽器相同版本的chromedriver,解壓縮後放到webdriver目錄裡") print("請下在面的網址下載與你chrome瀏覽器相同版本的chromedriver,解壓縮後放到webdriver目錄裡")
@ -373,6 +386,8 @@ def load_chromdriver_normal(webdriver_path, driver_type, adblock_plus_enable, he
driver = webdriver.Chrome(service=chrome_service, options=chrome_options, desired_capabilities=caps) driver = webdriver.Chrome(service=chrome_service, options=chrome_options, desired_capabilities=caps)
except Exception as exc: except Exception as exc:
error_message = str(exc) error_message = str(exc)
if show_debug_message:
print(exc)
left_part = None left_part = None
if "Stacktrace:" in error_message: if "Stacktrace:" in error_message:
left_part = error_message.split("Stacktrace:")[0] left_part = error_message.split("Stacktrace:")[0]
@ -397,7 +412,18 @@ def load_chromdriver_normal(webdriver_path, driver_type, adblock_plus_enable, he
return driver return driver
def load_chromdriver_uc(webdriver_path, adblock_plus_enable, headless): def load_chromdriver_uc(config_dict):
show_debug_message = True # debug.
show_debug_message = False # online
if config_dict["advanced"]["verbose"]:
show_debug_message = True
Root_Dir = get_app_root()
webdriver_path = os.path.join(Root_Dir, "webdriver")
adblock_plus_enable = config_dict["advanced"]["adblock_plus_enable"]
headless = config_dict["advanced"]["headless"]
import undetected_chromedriver as uc import undetected_chromedriver as uc
chromedriver_path = get_chromedriver_path(webdriver_path) chromedriver_path = get_chromedriver_path(webdriver_path)
@ -443,7 +469,8 @@ def load_chromdriver_uc(webdriver_path, adblock_plus_enable, headless):
driver = uc.Chrome(driver_executable_path=chromedriver_path, options=options, desired_capabilities=caps) driver = uc.Chrome(driver_executable_path=chromedriver_path, options=options, desired_capabilities=caps)
except Exception as exc: except Exception as exc:
error_message = str(exc) error_message = str(exc)
#print(exc) if show_debug_message:
print(exc)
left_part = None left_part = None
if "Stacktrace:" in error_message: if "Stacktrace:" in error_message:
left_part = error_message.split("Stacktrace:")[0] left_part = error_message.split("Stacktrace:")[0]
@ -460,7 +487,8 @@ def load_chromdriver_uc(webdriver_path, adblock_plus_enable, headless):
driver = uc.Chrome(options=options, desired_capabilities=caps) driver = uc.Chrome(options=options, desired_capabilities=caps)
except Exception as exc: except Exception as exc:
error_message = str(exc) error_message = str(exc)
#print(exc) if show_debug_message:
print(exc)
left_part = None left_part = None
if "Stacktrace:" in error_message: if "Stacktrace:" in error_message:
left_part = error_message.split("Stacktrace:")[0] left_part = error_message.split("Stacktrace:")[0]
@ -478,7 +506,7 @@ def load_chromdriver_uc(webdriver_path, adblock_plus_enable, headless):
print("Unable to use undetected_chromedriver, ") print("Unable to use undetected_chromedriver, ")
print("try to use local chromedriver to launch chrome browser.") print("try to use local chromedriver to launch chrome browser.")
driver_type = "selenium" driver_type = "selenium"
driver = load_chromdriver_normal(webdriver_path, driver_type, adblock_plus_enable, headless) driver = load_chromdriver_normal(config_dict, driver_type)
else: else:
print("建議您自行下載 ChromeDriver 到 webdriver 的資料夾下") print("建議您自行下載 ChromeDriver 到 webdriver 的資料夾下")
print("you need manually download ChromeDriver to webdriver folder.") print("you need manually download ChromeDriver to webdriver folder.")
@ -538,7 +566,7 @@ def get_driver_by_config(config_dict):
if browser == "chrome": if browser == "chrome":
# method 6: Selenium Stealth # method 6: Selenium Stealth
if driver_type != CONST_WEBDRIVER_TYPE_UC: if driver_type != CONST_WEBDRIVER_TYPE_UC:
driver = load_chromdriver_normal(webdriver_path, driver_type, adblock_plus_enable, headless) driver = load_chromdriver_normal(config_dict, driver_type)
else: else:
# method 5: uc # method 5: uc
# multiprocessing not work bug. # multiprocessing not work bug.
@ -546,7 +574,7 @@ def get_driver_by_config(config_dict):
if hasattr(sys, 'frozen'): if hasattr(sys, 'frozen'):
from multiprocessing import freeze_support from multiprocessing import freeze_support
freeze_support() freeze_support()
driver = load_chromdriver_uc(webdriver_path, adblock_plus_enable, headless) driver = load_chromdriver_uc(config_dict)
if browser == "firefox": if browser == "firefox":
# default os is linux/mac # default os is linux/mac