2022-11-08, for tixcraft accept all cookie, close all alert

master
CHUN YU YAO 2022-11-09 18:44:29 +08:00
parent bbcfcf2f56
commit 856c667451
2 changed files with 70 additions and 26 deletions

View File

@ -67,7 +67,7 @@ ssl._create_default_https_context = ssl._create_unverified_context
#附註1沒有寫的很好很多地方應該可以模組化。 #附註1沒有寫的很好很多地方應該可以模組化。
#附註2 #附註2
CONST_APP_VERSION = u"MaxBot (2022.11.07)" CONST_APP_VERSION = u"MaxBot (2022.11.08)"
CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom" CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom"
CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top" CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top"
@ -285,6 +285,17 @@ def load_chromdriver_uc(webdriver_path):
return driver return driver
def close_browser_tabs(driver):
if not driver is None:
try:
window_handles_count = len(driver.window_handles)
if window_handles_count >= 1:
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
except Exception as excSwithFail:
pass
def load_config_from_local(driver): def load_config_from_local(driver):
config_dict = get_config_dict() config_dict = get_config_dict()
@ -547,16 +558,10 @@ def load_config_from_local(driver):
driver = webdriver.Firefox(service=firefox_service) driver = webdriver.Firefox(service=firefox_service)
time.sleep(3.0) time.sleep(15.0)
#print("try to close opened tabs.") #print("try to close opened tabs.")
try: for i in range(2):
window_handles_count = len(driver.window_handles) close_browser_tabs(driver)
if window_handles_count >= 1:
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
except Exception as excSwithFail:
pass
if driver is None: if driver is None:
print("create web driver object fail @_@;") print("create web driver object fail @_@;")
@ -564,23 +569,15 @@ def load_config_from_local(driver):
try: try:
print("goto url:", homepage) print("goto url:", homepage)
driver.get(homepage) driver.get(homepage)
except WebDriverException: except WebDriverException as exce2:
print('oh no not again !') print('oh no not again, WebDriverException')
except Exception as exec1: print('WebDriverException:', exce2)
print('get() raise Exception:', exec1) except Exception as exce1:
print('get URL Exception:', exec1)
pass pass
if driver is None: for i in range(2):
time.sleep(1.0) close_browser_tabs(driver)
#print("try to close opened tabs.")
try:
window_handles_count = len(driver.window_handles)
if window_handles_count >= 1:
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
except Exception as excSwithFail:
pass
else: else:
@ -932,6 +929,45 @@ def get_answer_list_by_question(captcha_text_div_text):
#print("return_list:", return_list) #print("return_list:", return_list)
return return_list, my_answer_delimitor return return_list, my_answer_delimitor
# close some div on home url.
def tixcraft_home(driver):
accept_all_cookies_btn = None
try:
accept_all_cookies_btn = driver.find_element(By.ID, 'onetrust-accept-btn-handler')
except Exception as exc:
#print("find accept_all_cookies_btn fail")
pass
if accept_all_cookies_btn is not None:
if accept_all_cookies_btn.is_enabled() and accept_all_cookies_btn.is_displayed():
try:
accept_all_cookies_btn.click()
except Exception as exc:
print("try to click accept_all_cookies_btn fail")
try:
driver.execute_script("arguments[0].click();", accept_all_cookies_btn)
except Exception as exc:
pass
close_all_alert_btns = None
try:
close_all_alert_btns = driver.find_elements(By.CSS_SELECTOR, "[class='close-alert']")
except Exception as exc:
print("find close_all_alert_btns fail")
if close_all_alert_btns is not None:
#print('alert count:', len(close_all_alert_btns))
for alert_btn in close_all_alert_btns:
if alert_btn.is_enabled() and alert_btn.is_displayed():
try:
alert_btn.click()
except Exception as exc:
print("try to click alert_btn fail")
try:
driver.execute_script("arguments[0].click();", alert_btn)
except Exception as exc:
pass
# from detail to game # from detail to game
def tixcraft_redirect(driver, url): def tixcraft_redirect(driver, url):
ret = False ret = False
@ -3543,6 +3579,14 @@ def main():
tixcraft_family = True tixcraft_family = True
if tixcraft_family: if tixcraft_family:
if url == 'https://tixcraft.com/':
tixcraft_home(driver)
continue
if url == 'https://indievox.com/':
tixcraft_home(driver)
continue
#print("tixcraft_family entry.") #print("tixcraft_family entry.")
if '/ticket/order' in url: if '/ticket/order' in url:
# do nothing. # do nothing.

View File

@ -19,7 +19,7 @@ import sys
import platform import platform
import json import json
CONST_APP_VERSION = u"MaxBot (2022.11.07)" CONST_APP_VERSION = u"MaxBot (2022.11.08)"
CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom" CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom"
CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top" CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top"