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

Selenium VBA无法定位元素求助:需两次检查才获目标元素代码

解答:动态DOM加载问题与Selenium VBA定位隐藏元素

Hey Lucas, let's tackle your two main questions one by one—this is a super common issue with dynamic websites, so you're not alone!

一、为什么需要两次检查才能获取目标代码?

The core reason here is dynamic DOM rendering tied to user interaction:

  • When you first click "Inspect" right after clicking "By Round" (or its Portuguese equivalent "Por Rodada"), the browser's DevTools might capture the DOM state before the target element finishes loading. Many modern sites use JavaScript to inject elements into the page only after a user action (like a click), so there's a tiny delay between your click and the element appearing in the DOM.
  • Your first inspection might have selected a parent container or placeholder element instead of the actual target. The second time you inspect, the element has fully loaded and is present in the DOM, so DevTools picks it up correctly.
  • Another possibility is that the DevTools "Elements" tab was still showing the pre-click DOM snapshot. Refreshing the tab or re-inspecting after a short wait ensures you're looking at the updated DOM.

二、如何在Selenium VBA代码中定位该元素?

To reliably locate elements that load dynamically (even if they're hidden initially), follow these steps:

1. First: Trigger the element's loading

Before trying to locate the target element, simulate clicking the "By Round" button to trigger the DOM update. Use the correct Portuguese text or a stable class-based selector for this button:

Dim driver As New ChromeDriver
driver.Get "https://www.sofascore.com/tournament/football/england/premier-league/17"

' Click the "Por Rodada" (By Round) button first
Dim byRoundBtn As WebElement
On Error Resume Next
Set byRoundBtn = driver.FindElementByXPath("//button[contains(text(), 'Por Rodada')]")
On Error GoTo 0
If Not byRoundBtn Is Nothing Then
    byRoundBtn.Click
Else
    ' Fallback: Use class-based selector if text is inconsistent
    Set byRoundBtn = driver.FindElementByXPath("//div[contains(@class, 'TournamentHeader__tabs')]//button[contains(@class, 'Tab')]")
    byRoundBtn.Click
End If

2. Wait for the element to load (critical!)

Never assume the element is ready immediately after clicking. Use Selenium's built-in waiters to wait for the element to exist or become visible:

' Wait up to 10 seconds for the target element to exist
driver.Waiter.Timeout = 10
driver.Waiter.Until ElementExists(By.XPath("your_target_xpath_here"))

3. Locate hidden elements

If the element is hidden (e.g., display: none or visibility: hidden), Selenium's standard FindElement might still work, but if not, use JavaScript to access it directly:

' Option 1: Standard FindElement (works for most hidden elements)
Dim targetElement As WebElement
Set targetElement = driver.FindElementByXPath("//div[contains(@class, 'RoundSelector__rounds')]//div[text()='1']")

' Option 2: Use JavaScript to get the element if standard method fails
Dim hiddenElement As Object
Set hiddenElement = driver.ExecuteScript("return document.querySelector('div.RoundSelector__rounds div');")

4. Tips for writing stable selectors

Avoid using dynamic attributes like @id="random-123" or @data-key="abc456"—these change on page load. Instead:

  • Use text-based XPaths (e.g., //button[contains(text(), 'Por Rodada')])
  • Combine static classes (e.g., //div[contains(@class, 'RoundSelector')]//select)
  • Use relative paths instead of absolute paths (absolute paths are fragile if the DOM structure changes)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:57:51