You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

解决Python+Selenium爬取Aliexpress已发货订单时的登录检测问题——如何隐藏Selenium特征

如何隐藏Selenium特征以绕过Aliexpress和Google的检测?

我太懂你的困扰了——Selenium默认带的那些“自动化标识”简直就是给反爬机制递刀子,像Aliexpress、Google这种平台的反爬系统一抓一个准。别担心,我给你几个亲测有效的方案,一步步帮你把脚本伪装成真实用户的操作。

方案一:手动修改ChromeOptions,核心特征全隐藏

Selenium会给Chrome留下好几个明显的“破绽”,我们直接从启动配置和浏览器属性下手,把这些破绽补上:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

# 配置Chrome的反检测选项
chrome_options = Options()
# 禁用自动化扩展的提示弹窗
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
# 直接关掉Selenium自带的自动化扩展
chrome_options.add_experimental_option('useAutomationExtension', False)
# 阻止Chrome内部检测到自动化控制
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

path='C:\\chromedriver.exe'
driver = webdriver.Chrome(path, options=chrome_options)

# 用JS把浏览器的webdriver属性改成undefined(这是很多网站检测的核心点)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

driver.get("https://aliexpress.com/orderList")
time.sleep(2)  # 模拟用户等待页面加载的时间

email = driver.find_element_by_name('fm-login-id')
print("找到邮箱输入框")
email.send_keys("你的邮箱")
time.sleep(1)  # 输入完邮箱停一下,别太急

password = driver.find_element_by_name('fm-login-password')
password.send_keys("你的密码")
time.sleep(1)

login_btn = driver.find_element_by_class_name('fm-button')
login_btn.click()

这些配置的作用:

  • 去掉Selenium的自动化提示,避免暴露身份
  • 禁用专门的自动化扩展,减少特征
  • 用JS篡改navigator.webdriver属性,这是绝大多数反爬脚本会检测的关键标识

方案二:用undetected-chromedriver,一键搞定所有隐藏

如果手动配置太麻烦,直接用这个专门为反爬设计的库——它会自动帮你处理所有Selenium特征的隐藏,甚至能应对一些复杂的检测逻辑。

先安装库:

pip install undetected-chromedriver

然后替换你的代码:

import undetected_chromedriver as uc
import time

# 初始化浏览器,这个库已经帮你做好所有隐藏配置了
driver = uc.Chrome()

driver.get("https://aliexpress.com/orderList")
time.sleep(2)

email = driver.find_element_by_name('fm-login-id')
print("找到邮箱输入框")
email.send_keys("你的邮箱")
time.sleep(1)

password = driver.find_element_by_name('fm-login-password')
password.send_keys("你的密码")
time.sleep(1)

login_btn = driver.find_element_by_class_name('fm-button')
login_btn.click()

这个库的优势在于:它会自动修改Chrome的启动参数、移除webdriver标识,甚至会处理一些简单的验证码触发场景,是目前绕过这类检测最省心的方案。

方案三:模拟真实用户的操作习惯,降低被怀疑概率

就算特征隐藏得再好,操作太机械也会被盯上。给你的脚本加一点“人味”:

  • 随机延迟代替固定的time.sleep,比如random.uniform(1,3)
  • 模拟鼠标移动到元素上再点击,而不是直接定位就操作
  • 输入账号密码时,逐个字符输入,每个字符之间加小延迟
  • 偶尔模拟滚动页面、点击无关元素的操作

举个优化后的代码例子:

from selenium.webdriver.common.action_chains import ActionChains
import random
import time
import undetected_chromedriver as uc

driver = uc.Chrome()
driver.get("https://aliexpress.com/orderList")
# 随机等待2-4秒,模拟用户看页面的时间
time.sleep(random.uniform(2, 4))

email_box = driver.find_element_by_name('fm-login-id')
# 先把鼠标移到输入框,再点击
ActionChains(driver).move_to_element(email_box).click().perform()
time.sleep(random.uniform(0.5, 1.5))

# 逐个输入邮箱字符,模拟真实打字速度
email = "你的邮箱"
for char in email:
    email_box.send_keys(char)
    time.sleep(random.uniform(0.1, 0.3))

time.sleep(random.uniform(1, 2))

password_box = driver.find_element_by_name('fm-login-password')
ActionChains(driver).move_to_element(password_box).click().perform()
time.sleep(random.uniform(0.5, 1.5))

# 同理,逐个输入密码
password = "你的密码"
for char in password:
    password_box.send_keys(char)
    time.sleep(random.uniform(0.1, 0.3))

time.sleep(random.uniform(1, 2))

login_btn = driver.find_element_by_class_name('fm-button')
ActionChains(driver).move_to_element(login_btn).click().perform()

最后提醒

如果还是遇到验证码或账号风控,可以试试:

  • 使用代理IP,避免同一IP频繁操作
  • 不要在短时间内重复登录同一个账号
  • 必要时可以手动完成一次验证码验证,之后保持会话

内容的提问来源于stack exchange,提问作者Kasiopea

火山引擎 最新活动