2023-09-12, support multi-question mode

master
CHUN YU YAO 2023-10-08 15:15:08 +08:00
parent 1e004e1c51
commit 014a3f5868
2 changed files with 98 additions and 50 deletions

View File

@ -55,7 +55,7 @@ import webbrowser
import chromedriver_autoinstaller import chromedriver_autoinstaller
CONST_APP_VERSION = "MaxBot (2023.09.11)" CONST_APP_VERSION = "MaxBot (2023.09.12)"
CONST_MAXBOT_CONFIG_FILE = "settings.json" CONST_MAXBOT_CONFIG_FILE = "settings.json"
CONST_MAXBOT_LAST_URL_FILE = "MAXBOT_LAST_URL.txt" CONST_MAXBOT_LAST_URL_FILE = "MAXBOT_LAST_URL.txt"
@ -2654,72 +2654,119 @@ def fill_common_verify_form(driver, config_dict, inferred_answer_string, fail_li
is_do_press_next_button = False is_do_press_next_button = False
form_input = None form_input_1 = None
form_input_2 = None
if form_input_count > 0: if form_input_count > 0:
form_input = form_input_list[0] form_input_1 = form_input_list[0]
if form_input_count > 1:
form_input_2 = form_input_list[1]
is_multi_question_mode = False
answer_list = get_answer_list_from_user_guess_string(config_dict)
if form_input_count == 1: if form_input_count == 1:
is_do_press_next_button = True is_do_press_next_button = True
else:
if form_input_count == 2:
if not form_input_2 is None:
if len(answer_list) >= 2:
if(len(answer_list[0]) > 0):
if(len(answer_list[1]) > 0):
is_multi_question_mode = True
inputed_value = None inputed_value_1 = None
if not form_input is None: if not form_input_1 is None:
try: try:
inputed_value = form_input.get_attribute('value') inputed_value_1 = form_input_1.get_attribute('value')
except Exception as exc: except Exception as exc:
if show_debug_message: if show_debug_message:
print("get_attribute of verify code fail") print("get_attribute of verify code fail")
pass pass
if inputed_value_1 is None:
inputed_value_1 = ""
if inputed_value is None: inputed_value_2 = None
inputed_value = "" if not form_input_2 is None:
try:
inputed_value_2 = form_input_2.get_attribute('value')
except Exception as exc:
if show_debug_message:
print("get_attribute of verify code fail")
pass
if inputed_value_2 is None:
inputed_value_2 = ""
is_answer_sent = False is_answer_sent = False
if not form_input is None: if not is_multi_question_mode:
if len(inferred_answer_string) > 0: if not form_input_1 is None:
if inputed_value != inferred_answer_string: if len(inferred_answer_string) > 0:
try: if inputed_value_1 != inferred_answer_string:
# PS: sometime may send key twice... try:
form_input.clear() # PS: sometime may send key twice...
form_input.send_keys(inferred_answer_string) form_input_1.clear()
is_button_clicked = False form_input_1.send_keys(inferred_answer_string)
if is_do_press_next_button: is_button_clicked = False
if submit_by_enter: if is_do_press_next_button:
form_input.send_keys(Keys.ENTER) if submit_by_enter:
is_button_clicked = True form_input_1.send_keys(Keys.ENTER)
if len(next_step_button_css) > 0: is_button_clicked = True
is_button_clicked = force_press_button(driver, By.CSS_SELECTOR, next_step_button_css) if len(next_step_button_css) > 0:
is_button_clicked = force_press_button(driver, By.CSS_SELECTOR, next_step_button_css)
if is_button_clicked: if is_button_clicked:
is_answer_sent = True is_answer_sent = True
fail_list.append(inferred_answer_string) fail_list.append(inferred_answer_string)
if show_debug_message:
print("sent password by bot:", inferred_answer_string, " at #", len(fail_list))
except Exception as exc:
if show_debug_message: if show_debug_message:
print("sent password by bot:", inferred_answer_string, " at #", len(fail_list)) print(exc)
except Exception as exc: pass
if show_debug_message:
print(exc)
pass
if is_answer_sent: if is_answer_sent:
for i in range(3): for i in range(3):
time.sleep(0.1) time.sleep(0.1)
alert_ret = check_pop_alert(driver) alert_ret = check_pop_alert(driver)
if alert_ret: if alert_ret:
if show_debug_message: if show_debug_message:
print("press accept button at time #", i+1) print("press accept button at time #", i+1)
break break
else: else:
# no answer to fill. # no answer to fill.
if len(inputed_value)==0: if len(inputed_value_1)==0:
try: try:
# solution 1: js. # solution 1: js.
driver.execute_script("if(!(document.activeElement === arguments[0])){arguments[0].focus();}", form_input) driver.execute_script("if(!(document.activeElement === arguments[0])){arguments[0].focus();}", form_input_1)
# solution 2: selenium. # solution 2: selenium.
#form_input.click() #form_input_1.click()
time.sleep(check_input_interval) time.sleep(check_input_interval)
except Exception as exc: except Exception as exc:
pass pass
else:
# multi question mode.
try:
if inputed_value_1 != answer_list[0]:
form_input_1.clear()
form_input_1.send_keys(answer_list[0])
if inputed_value_2 != answer_list[1]:
form_input_2.clear()
form_input_2.send_keys(answer_list[1])
is_button_clicked = False
form_input_2.send_keys(Keys.ENTER)
if len(next_step_button_css) > 0:
is_button_clicked = force_press_button(driver, By.CSS_SELECTOR, next_step_button_css)
if is_button_clicked:
is_answer_sent = True
fail_list.append(answer_list[0])
fail_list.append(answer_list[1])
if show_debug_message:
print("sent password by bot:", inferred_answer_string, " at #", len(fail_list))
except Exception as exc:
pass
return is_answer_sent, fail_list return is_answer_sent, fail_list
def get_answer_list_from_user_guess_string(config_dict): def get_answer_list_from_user_guess_string(config_dict):

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#encoding=utf-8 #encoding=utf-8
try: try:
@ -34,7 +35,7 @@ import ssl
ssl._create_default_https_context = ssl._create_unverified_context ssl._create_default_https_context = ssl._create_unverified_context
CONST_APP_VERSION = "MaxBot (2023.09.11)" CONST_APP_VERSION = "MaxBot (2023.09.12)"
CONST_MAXBOT_CONFIG_FILE = "settings.json" CONST_MAXBOT_CONFIG_FILE = "settings.json"
CONST_MAXBOT_LAST_URL_FILE = "MAXBOT_LAST_URL.txt" CONST_MAXBOT_LAST_URL_FILE = "MAXBOT_LAST_URL.txt"