You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Selenium加载墨西哥航空网站异常,如何解决?

问题分析与解决方案

你的问题源于墨西哥航空官网的反爬机制识别出了Selenium自动化驱动,导致页面未加载完整的前端资源(如CSS、交互JS),最终呈现无样式的原始状态且无法交互。

解决步骤

  • 添加ChromeOptions参数隐藏自动化特征,模拟真实浏览器环境
  • 配合显式等待确保页面核心交互元素加载完成

修改后的代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
# 隐藏自动化检测标识
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 设置真实浏览器的用户代理(可替换为你本地Chrome的UA值)
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")

driver = webdriver.Chrome(service=Service(driver_path), options=options)
# 移除浏览器端的webdriver标识
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

driver.get("https://aeromexico.com/es-mx")

# 显式等待核心交互元素加载完成(示例为搜索按钮,可根据实际需求调整)
WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "button[data-testid='search-button']"))
)

关键说明

  1. --disable-blink-features=AutomationControlled:禁用Chrome内置的自动化控制检测逻辑
  2. excludeSwitches与useAutomationExtension:关闭Selenium默认的自动化提示弹窗及扩展
  3. 自定义user-agent:避免被网站识别为爬虫类UA
  4. JS修改navigator.webdriver:移除浏览器环境中暴露自动化身份的标识
  5. 显式等待:确保页面资源加载完成后再执行交互操作,避免元素未找到的报错

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:40:25