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

SeleniumBase脚本非无头模式正常,无头模式报元素未找到错误

无头模式下SeleniumBase无法加载目标页面并定位元素的问题

我使用Python的SeleniumBase进行网页自动化交互,非无头模式下运行完全正常,但设置为无头模式(headless=True或headless2=True)时,即便多次尝试滚动,仍无法找到#card-lib-selectCompany-change等特定元素。

相关代码

from seleniumbase import SB

def scrape_servipag_service_reading(service_type, company, identifier):
    result = None
    with SB(uc=True, headless2=True) as sb:
        try:
            sb.set_window_size(1920, 1080)
            url = f"https://portal.servipag.com/paymentexpress/category/{service_type}"
            sb.uc_open_with_reconnect(url, 4)
            sb.uc_gui_click_captcha()
            sb.sleep(10)

            # Scroll to find element
            for _ in range(30):
                sb.execute_script("window.scrollBy(0, 100);")
                sb.sleep(0.2)
                if sb.is_element_visible("#card-lib-selectCompany-change"):
                    break

            sb.wait_for_element("#card-lib-selectCompany-change", timeout=100)
            sb.select_option_by_text("#card-lib-selectCompany-change", company)
            # More interactions follow...

        except Exception as e:
            print(f"Error: {e}")
    return result

报错信息

非无头模式下能正常找到并交互#card-lib-selectCompany-change元素,但无头模式下会报错:
Elemento #card-lib-selectCompany-change no se encontró después de varios intentos de desplazamiento.

已尝试的修复方法

  • 设置自定义user-agent
  • 显式设置视口大小为1920x1080
  • 增加滚动次数和等待间隔
  • 切换headless=True与headless2=True

在无头模式(headless=True)下添加打印语句后,发现页面未加载目标内容,当前URL为chrome-extension://neajdppkdcdipfabeoofebfddakdcjhd/audio.html,页面源码为无关内容。请问有人遇到过类似问题或知道如何确保无头模式下页面完全加载吗?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 13:00:05