如何用Python的Selenium完成谷歌账号认证?求绕过检测及替代方案
绕过谷歌机器人检测的解决方案及相关问题解答
一、优化现有Selenium代码的几个关键点
你的Firefox配置还有不少可以调整的地方,试试这些优化:
- 删掉冗余的
desired_capabilities设置,Selenium 4+版本里用FirefoxOptions就足够了 - 补充更多反检测的浏览器偏好:
profile.set_preference("dom.webnotifications.enabled", False) profile.set_preference("media.volume_scale", "0.0") profile.set_preference("browser.cache.disk.enable", False) profile.set_preference("browser.cache.memory.enable", False) profile.set_preference("browser.cache.offline.enable", False) profile.set_preference("network.http.use-cache", False) - 把固定的
sleep换成显式等待,避免等待时间不合理触发检测:# 替换原有的sleep(8)和find_element email = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'identifierId'))) - 模拟真实用户的输入节奏,不要一次性输完内容:
import random for char in "你的邮箱地址": email.send_keys(char) sleep(random.uniform(0.1, 0.3)) - 用
ActionChains模拟真实点击,代替直接调用click():from selenium.webdriver.common.action_chains import ActionChains nextBtn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'identifierNext'))) ActionChains(driver).move_to_element(nextBtn).click().perform()
二、非Selenium的替代方案
1. Playwright
微软出品的自动化工具,内置反检测机制,默认就能隐藏自动化特征,配置比Selenium简单得多,示例代码:
from playwright.sync_api import sync_playwright import random import time with sync_playwright() as p: browser = p.firefox.launch(headless=False) page = browser.new_page(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36") page.goto(url1) # 等待邮箱输入框加载完成 page.wait_for_selector('#identifierId', timeout=10000) email_input = page.locator('#identifierId') # 逐字符输入,模拟真人操作 for char in "你的邮箱地址": email_input.type(char, delay=random.uniform(0.1, 0.3)) time.sleep(random.uniform(0.05, 0.2)) # 点击下一步按钮 page.locator('#identifierNext').click() # 后续密码步骤可以同理处理 browser.close()
2. Requests + 抓包分析
如果能通过抓包工具解析谷歌登录的API接口,可以直接用requests库发送POST请求完成登录,但需要处理Session、Token、验证码等一系列验证逻辑,难度很高,不推荐新手尝试。
三、关闭JS脚本有用吗?
完全没用,甚至会帮倒忙。谷歌登录页面完全依赖JavaScript渲染和交互,关闭JS后页面会直接无法正常显示,连输入框都加载不出来。而且谷歌的机器人检测会检查页面JS是否正常运行,关闭JS反而会触发更严格的检测机制。
内容的提问来源于stack exchange,提问作者Мен Осы
相关产品推荐
相关产品推荐

