使用Selenium登录时Firefox提示“此浏览器或应用可能不安全”求助
问题:Selenium操控Firefox登录Gmail/Meet时触发“浏览器或应用可能不安全”提示
我用以下Selenium代码实现自动登录Gmail并进入Google Meet,代码在其他电脑上运行正常,但在我自己电脑上执行时,Firefox会弹出“此浏览器或应用可能不安全”的提示。试过网上的方案都没用,我是Selenium新手,代码是伙伴给的,求解决办法。
原代码如下:
AUTOMATION_FAILED = False USE_FAILSAFE_PERCAUTIONS = True MEET_CODE = "xxx-xxx-xxx" EMAIL_ADDRESS = "myaccount" AD_USERNAME = "" AD_PASSWORD = "mypasswrd." options = Options() options.headless = False options.set_preference('media.navigator.permission.disabled', True) profile = webdriver.FirefoxProfile() binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe") # Absolute path to Firefox executable driver = webdriver.Firefox(profile, options=options, firefox_binary=binary) driver.maximize_window() driver.get( "https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1#identifier") print("Successfully loaded Google Authetication point! [Gmail]") time.sleep(2) if AUTOMATION_FAILED == False: for i in range(6): try: driver.find_element("id", "identifierId").send_keys(EMAIL_ADDRESS) driver.find_element("id", "identifierNext").click() print("Sucessfully uploaded email...") break except NoSuchElementException: print("[ERROR]: Attempting to resend email address.") if USE_FAILSAFE_PERCAUTIONS: time.sleep(6) else: driver.implicitly_wait(6) except WebDriverException as e: print("[ERROR]: Web driver error.\n[ERROR DETAILS]:", e) AUTOMATION_FAILED = True break if AUTOMATION_FAILED == False: if AD_USERNAME == "" or AD_USERNAME == None: for i in range(6): try: driver.find_element("name", "Passwd").send_keys(AD_PASSWORD) driver.find_element("id", "passwordNext").click() print("Sucessfully sent credentials...") break except NoSuchElementException: print("[ERROR]: Attempting to find password input.") if USE_FAILSAFE_PERCAUTIONS: time.sleep(6) else: driver.implicitly_wait(6) else: for i in range(6): try: driver.find_element("id", "userNameInput").send_keys(AD_USERNAME) driver.find_element("id", "passwordInput").send_keys(AD_PASSWORD) driver.find_element("id", "passwordNext").click() print("Sucessfully sent Active Directory credentials...") break except NoSuchElementException: print("[ERROR]: Attempting to find active directory login elements.") if USE_FAILSAFE_PERCAUTIONS: time.sleep(6) else: driver.implicitly_wait(6) except WebDriverException as e: print("[ERROR]: Web driver error.\n[ERROR DETAILS]:", e) AUTOMATION_FAILED = True break time.sleep(6) print("Loading Google Meets...") driver.get(f"https://meet.google.com/{MEET_CODE}") # driver.refresh()
解决办法
Google会通过检测浏览器的自动化特征拦截操作,以下是针对性的调整方案:
1. 隐藏自动化标识
在初始化options时添加以下配置,直接屏蔽Selenium的特征标识:
options.set_preference("dom.webdriver.enabled", False) options.set_preference('useAutomationExtension', False) options.add_argument("--disable-blink-features=AutomationControlled")
2. 使用本地已登录的Firefox配置文件
直接调用你日常使用的Firefox配置文件(带着已登录的Gmail状态),完全规避登录检测:
- 打开Firefox,地址栏输入
about:profiles,复制你的默认配置文件路径 - 替换代码中
profile的初始化部分:
这种方式启动的Firefox会保留你日常的登录状态,无需再输入账号密码。# 替换原来的 profile = webdriver.FirefoxProfile() profile_path = r"C:\Users\你的用户名\AppData\Roaming\Mozilla\Firefox\Profiles\xxxxx.default-release" profile = webdriver.FirefoxProfile(profile_path)
3. 优化等待逻辑
替换代码中所有time.sleep()为显式等待,更贴近真实用户操作节奏,减少被检测概率:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 示例:等待邮箱输入框加载完成再输入 WebDriverWait(driver, 10).until(EC.presence_of_element_located(("id", "identifierId"))).send_keys(EMAIL_ADDRESS)
修改后的完整代码
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.common.exceptions import NoSuchElementException, WebDriverException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time AUTOMATION_FAILED = False USE_FAILSAFE_PERCAUTIONS = True MEET_CODE = "xxx-xxx-xxx" EMAIL_ADDRESS = "myaccount" AD_USERNAME = "" AD_PASSWORD = "mypasswrd." options = Options() options.headless = False options.set_preference('media.navigator.permission.disabled', True) # 新增规避检测的配置 options.set_preference("dom.webdriver.enabled", False) options.set_preference('useAutomationExtension', False) options.add_argument("--disable-blink-features=AutomationControlled") # 使用本地已登录的Firefox配置文件 profile_path = r"C:\Users\你的用户名\AppData\Roaming\Mozilla\Firefox\Profiles\xxxxx.default-release" profile = webdriver.FirefoxProfile(profile_path) binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe") driver = webdriver.Firefox(profile, options=options, firefox_binary=binary) driver.maximize_window() # 直接跳转Meet(已登录状态下无需走Gmail登录流程) driver.get(f"https://meet.google.com/{MEET_CODE}") print("Loading Google Meets...") # 若需保留登录逻辑(未使用已登录配置文件时启用) # driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1#identifier") # print("Successfully loaded Google Authetication point! [Gmail]") # if AUTOMATION_FAILED == False: # try: # WebDriverWait(driver, 10).until(EC.presence_of_element_located(("id", "identifierId"))).send_keys(EMAIL_ADDRESS) # WebDriverWait(driver, 10).until(EC.element_to_be_clickable(("id", "identifierNext"))).click() # print("Sucessfully uploaded email...") # except NoSuchElementException: # print("[ERROR]: Failed to find email input.") # AUTOMATION_FAILED = True # except WebDriverException as e: # print("[ERROR]: Web driver error.\n[ERROR DETAILS]:", e) # AUTOMATION_FAILED = True # if AUTOMATION_FAILED == False and (AD_USERNAME == "" or AD_USERNAME == None): # try: # WebDriverWait(driver, 10).until(EC.presence_of_element_located(("name", "Passwd"))).send_keys(AD_PASSWORD) # WebDriverWait(driver, 10).until(EC.element_to_be_clickable(("id", "passwordNext"))).click() # print("Sucessfully sent credentials...") # except NoSuchElementException: # print("[ERROR]: Failed to find password input.") # AUTOMATION_FAILED = True # except WebDriverException as e: # print("[ERROR]: Web driver error.\n[ERROR DETAILS]:", e) # AUTOMATION_FAILED = True
内容的提问来源于stack exchange,提问作者Miguel Gallego
相关产品推荐
相关产品推荐

