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

Excel VBA无法访问realcommercial.com.au并提取数据,求解决方案

解决Excel VBA访问realcommercial.com.au被拦截及Shell启动后交互问题

问题背景

此前一直使用Excel VBA结合ChromeDriver访问realcommercial.com.au提取数据,近期代码突然失效,推测是目标网站拦截了自动化访问。原代码访问谷歌等网站正常,但无法打开目标网站;改用Shell命令启动浏览器可访问该网站,但无法与已启动页面交互提取数据。

原ChromeDriver代码(无法打开目标网站)

Sub SetChromeProfileLast()
    ' Uses ChromeDriver to launch new Chrome Window with Profile 1, passes URL, but does not open RealCommercial site.
Dim driver As New WebDriver
    driver.SetProfile "C:\Users\bergs\AppData\Local\Google\Chrome\User Data\Profile 1"
    driver.AddArgument "profile-directory=Profile 1"
    driver.AddArgument "user-data-dir"
    driver.Start "Chrome"

    driver.Get "https://realcommercial.com.au"
   
    Application.Wait Now + TimeValue("00:0:10")

End Sub

Shell启动代码(可访问但无法交互)

Sub ChromeInShell()
' Uses Shell to launch new Window, passes URL, opens RealCommercial site - can not interact with window.
Dim driver As New WebDriver
Dim url_name As String
 
    url_name = "https:\\realcommercial.com.au"

    Shell "C:\Program Files\Google\Chrome\Application\chrome.exe " & url_name
        
End Sub

需求

寻求以下任一解决方案:

  • a) 通过Start "Chrome"命令启动浏览器,避免被目标网站识别为自动化软件;
  • b) 实现与Shell命令启动的网页交互并爬取数据。

解决方案

方案a:优化ChromeDriver配置绕过自动化检测

通过调整Chrome启动参数和隐藏自动化特征,模拟真实浏览器行为:

Sub SetChromeProfileLast()
    Dim driver As New WebDriver
    ' 添加参数禁用自动化检测、模拟真实浏览器环境
    driver.AddArgument "--disable-blink-features=AutomationControlled"
    driver.AddArgument "--start-maximized"
    driver.AddArgument "--no-sandbox"
    driver.AddArgument "--disable-dev-shm-usage"
    driver.AddArgument "--disable-extensions"
    ' 正确配置用户数据目录和Profile路径
    driver.AddArgument "user-data-dir=C:\Users\bergs\AppData\Local\Google\Chrome\User Data"
    driver.AddArgument "profile-directory=Profile 1"
    
    driver.Start "Chrome"
    ' 执行脚本强制隐藏webdriver属性
    driver.ExecuteScript "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
    
    driver.Get "https://realcommercial.com.au"
    Application.Wait Now + TimeValue("00:00:10")
End Sub

关键调整说明:

  • --disable-blink-features=AutomationControlled:关闭浏览器内置的自动化检测机制,让navigator.webdriver默认返回undefined
  • 修正user-data-dir参数:需要指定完整的用户数据目录路径,再单独指定profile-directory,避免配置冲突
  • 额外执行脚本确保navigator.webdriver属性被隐藏,双重保险绕过检测

方案b:连接已启动的Chrome实例实现交互

通过给Shell启动的Chrome添加调试端口,让ChromeDriver连接到现有实例:

1. 修改Shell启动代码,开启调试端口

Sub ChromeInShell()
    Dim url_name As String
    url_name = "https://realcommercial.com.au"
    ' 启动Chrome时开启远程调试端口9222
    Shell "C:\Program Files\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222 " & url_name
End Sub

2. 编写连接及交互代码

Sub ConnectToRunningChrome()
    Dim driver As New WebDriver
    ' 连接到本地9222端口的Chrome实例
    driver.SetCapability "debuggerAddress", "localhost:9222"
    driver.Start "Chrome"
    
    ' 示例:获取页面标题
    MsgBox "页面标题:" & driver.Title
    ' 示例:提取指定元素文本(根据实际需求修改选择器)
    Dim targetText As String
    targetText = driver.FindElementByCssSelector(".some-class").Text
    MsgBox "提取到的文本:" & targetText
End Sub

说明:

  • --remote-debugging-port=9222:让Chrome启动时开启远程调试模式,允许外部程序连接控制
  • 连接后可使用标准WebDriver方法(FindElementBy...、ExecuteScript等)提取数据,操作逻辑和直接启动ChromeDriver一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 09:10:56