from selenium import webdriver from selenium_stealth import stealth 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.webdriver.chrome.service import Service as ChromeService import json # 读取配置 f = open('config.json', 'r') content = f.read() f.close() data = json.loads(content) executablePath = data["executable_path"] # 获取token def get_token(email, password, proxy_ip, proxy_port): # 设置代理 # proxy = Proxy() # proxy.proxy_type = ProxyType.MANUAL # proxy.http_proxy = f"{proxy_ip}:{proxy_port}" # proxy.ssl_proxy = f"{proxy_ip}:{proxy_port}" # 浏览器配置 options = webdriver.ChromeOptions() # options.add_argument('--proxy-server=http://{}:{}'.format(proxy_ip, proxy_port)) options.add_experimental_option("detach", True) options.add_experimental_option('useAutomationExtension', False) options.add_experimental_option("excludeSwitches", ['enable-automation']) options.add_argument('--start-maximized') options.add_argument('disable-infobars') options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') options.add_argument('--disable-dev-shm-usage') # service service = ChromeService(executable_path=executablePath) driver = webdriver.Chrome(service=service, options=options) stealth( driver, user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36", languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel Iris OpenGL Engine", fix_hairline=True, ) # with open('stealth.min.js', 'r') as f: # js = f.read() # # 调用函数在页面加载前执行脚本 # driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': js}) try: driver.get( "https://account.weverse.io/zh-CN/login/redirect?client_id=weverse&redirect_uri=https%3A%2F%2Fweverse.io%2FloginResult%3Ftopath%3D%252F") element = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.NAME, 'email')) ) driver.find_element(By.NAME, value="email").send_keys(email) driver.find_element(By.NAME, value="password").send_keys(password) driver.find_element(By.XPATH, value='//button[@type="submit"]').click() btn = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.CLASS_NAME, "body")) ) cookies = driver.get_cookies() access_token_value = None # 遍历列表,找到 name 为 'we2_access_token' 的字典 for cookie in cookies: if cookie['name'] == 'we2_access_token': access_token_value = cookie['value'] break # 找到后退出循环 return access_token_value except Exception as e: return None finally: driver.quit()