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

VBA中使用Selenium操作Chrome时的类型不匹配问题及PeerComparison表格表头行引用方案咨询

VBA Selenium ChromeDriver: Fixing Type Mismatch When Retrieving Table Header Row

The "Type Mismatch" error you’re facing boils down to a core difference between how IE’s native DOM automation and Selenium ChromeDriver handle table elements. Let’s break this down and get your code working:

Why the Error Occurs

In IE’s automation model, the Rows method returns TableRow objects directly, which aligns with your variable declaration. But with Selenium ChromeDriver:

  • FindElementsByTag("tr") returns a collection of WebElement objects, not TableRow instances.
  • Your variable PeerComparisonTableHeaderRow is declared as Selenium.TableRow, which isn’t compatible with the WebElement returned by ChromeDriver—this is exactly what triggers the type mismatch.

Solution: Adjust Variable Type & Use Selenium’s Standard API

Replace the TableRow type with WebElement (the universal element type in Selenium) and tweak your code to work with ChromeDriver’s methods:

Sub RetrievePeerComparisonHeader()
    Dim ChBrowser As Selenium.ChromeDriver
    Set ChBrowser = New Selenium.ChromeDriver
    
    Dim PeerComparisonTable As Selenium.WebElement
    Dim PeerComparisonTableHeaderRow As Selenium.WebElement ' Updated to WebElement
    
    ' Initialize Chrome and navigate to your target page
    ChBrowser.Start "chrome"
    ChBrowser.Get "https://www.screener.in/company/DIVISLAB/consolidated/"
    
    ' Get the second table (note: Selenium uses 0-based indexing)
    Set PeerComparisonTable = ChBrowser.FindElementsByTag("table")(1)
    
    ' Get the second row (adjust the index if your header sits in a different position)
    Set PeerComparisonTableHeaderRow = PeerComparisonTable.FindElementsByTag("tr")(1)
    
    ' Example: Verify success by printing the first header cell's text
    Debug.Print PeerComparisonTableHeaderRow.FindElementsByTag("th")(0).Text
    
    ' Clean up the browser session
    ChBrowser.Quit
End Sub

Bonus: More Reliable Table Locator

Index-based locators (like (1)) are fragile—if the page structure changes, your code will break. Instead, use a CSS selector or XPath to target the Peer Comparison table directly. For example, if the table has a unique class (check the page’s HTML source), use:

' Replace with the actual class name from the page's HTML
Set PeerComparisonTable = ChBrowser.FindElementByCss("table.peer-comparison")

This ensures you’re always targeting the correct table, no matter how other tables on the page are ordered.

Key Difference from IE Automation Recap

  • IE’s DOM has native Rows/Cells methods for tables; Selenium ChromeDriver relies on standard FindElement/FindElements calls to locate rows and cells.
  • Selenium uses WebElement as the base type for all page elements, rather than specialized types like TableRow.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 22:02:45