1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import StaleElementReferenceException, TimeoutException, NoSuchElementException import time
dict={ "0": "━━━━━", "1": "·━━━━", "2": "··━━━", "3": "···━━", "4": "····━", "5": "·····", "6": "━····", "7": "━━···", "8": "━━━··", "9": "━━━━·" }
driver = webdriver.Chrome()
url = 'http://127.0.0.1:9801/'
morse_codes=[]
nums=[]
def get_morse_code(morse_codes): nums=[] for morse in morse_codes: for num, ms in dict.items(): if morse == ms: nums.append(int(num)) print("nums:",nums) return nums
def input1(nums, button_selectors): for i, num in enumerate(nums): success = click_button_safely(num, button_selectors) if success: print(f"第{i+1}次输入{num},成功") else: print(f"第{i+1}次输入{num},失败") time.sleep(0.1)
def printhtml(): html_content = driver.page_source print("网页内容: ",html_content)
def get_button_selectors(): selectors = { 0: '#inputContent > div.inputContentBtnArea > div:nth-child(11)', 1: '#inputContent > div.inputContentBtnArea > div:nth-child(7)', 2: '#inputContent > div.inputContentBtnArea > div:nth-child(8)', 3: '#inputContent > div.inputContentBtnArea > div:nth-child(9)', 4: '#inputContent > div.inputContentBtnArea > div:nth-child(4)', 5: '#inputContent > div.inputContentBtnArea > div:nth-child(5)', 6: '#inputContent > div.inputContentBtnArea > div:nth-child(6)', 7: '#inputContent > div.inputContentBtnArea > div:nth-child(1)', 8: '#inputContent > div.inputContentBtnArea > div:nth-child(2)', 9: '#inputContent > div.inputContentBtnArea > div:nth-child(3)' } return selectors
def click_button_safely(num, selectors, max_retries=3): for attempt in range(max_retries): try: button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, selectors[num])) ) button.click() return True except (StaleElementReferenceException, TimeoutException, NoSuchElementException) as e: print(f"点击按钮 {num} 第 {attempt + 1} 次尝试失败: {type(e).__name__}") if attempt < max_retries - 1: time.sleep(0.5) else: print(f"点击按钮 {num} 最终失败: {e}") return False except Exception as e: print(f"点击按钮 {num} 发生未知错误: {e}") return False
def main(): driver.get(url) time.sleep(2) start_bottom = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'decipherComputerChallengeStartBTN')) ) start_bottom.click() print("开始挑战!") time.sleep(0.2) button_selectors = get_button_selectors()
content_item = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, '#leftPanel > div.codeShow > div.codeContent')) ) clean_morse_code = content_item.text.replace('\n', '').replace(' ', '') print("clean_morse_code:", clean_morse_code) morse_codes=[clean_morse_code[i:i+5] for i in range(0, len(clean_morse_code), 5)] print("morse_code:",morse_codes)
for morse in morse_codes: for num, ms in dict.items(): if morse == ms: nums.append(int(num)) print("nums:",nums)
for i, num in enumerate(nums): success = click_button_safely(num, button_selectors) if success: print(f"第{i+1}次输入{num},成功") else: print(f"第{i+1}次输入{num},失败") time.sleep(0.1) time.sleep(10) driver.quit()
if __name__ == '__main__': main()
|