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
| 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 import time
def findHub(driver,element,by=By.XPATH,timeout=5,times = 1): ''' element为选择器表达式 timeout为要素寻找超时时间,默认5秒 times为寻找次数,默认3次 by 可选参数 ID = "id" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" NAME = "name" TAG_NAME = "tag name" CLASS_NAME = "class name" CSS_SELECTOR = "css selector" ''' try: ele = WebDriverWait(driver, timeout).until( EC.presence_of_element_located((by, element)) ) ele.click() time.sleep(3) return True except Exception as e: if times != 0: print("找不到要素",element, "。重试第", times, "次。") result = findHub(driver, element, by, timeout, times - 1) else : return False return result
def unlockWallet(driver, password): driver.get("chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/popup.html") time.sleep(5) driver.find_element_by_id('password').send_keys(password) findHub(driver, 'button', By.TAG_NAME)
def claim(account): # 小狐狸钱包解锁密码 password = '' # 浏览器驱动目录 driverPath = r'D:\bin\chromedriver' # 浏览器缓存数据,包括收藏夹,扩展,记录的密码,cookies等 userDataPath = r'user-data-dir=D:\bin\\'+account options = webdriver.ChromeOptions() options.add_argument(userDataPath) driver = webdriver.Chrome(executable_path=driverPath, options=options) unlockWallet(driver, password) driver.get(r"https://zapper.fi/quests") assert "Zapper" in driver.title findHub(driver, "//button[text()='Connect Wallet']") findHub(driver, "//button/span[text()='MetaMask']") handles = driver.window_handles if len(handles) > 1: driver.switch_to.window(handles[1]) time.sleep(5) findHub(driver, "//button[text()='Next']") findHub(driver,"//button[text()='Connect']") driver.switch_to.window(handles[0]) findHub(driver,"//button[not(@disabled)]/span[text()='Claim']") handles = driver.window_handles if len(handles) > 1: driver.switch_to.window(handles[1]) time.sleep(5) findHub(driver, "//button[text()='Sign']") driver.switch_to.window(handles[0]) print("关闭浏览器") driver.close()
if __name__ == '__main__': while True: for x in range(1,10): claim('account'+str(x)) print("完成任务") time.sleep(24*3600)
|