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

Selenium C#网页爬取:无法获取竞价结束时间值

解决Selenium无法获取Bidrl竞价结束时间的问题

问题分析

你当前的代码无法获取Bidding Ends字段值,核心原因有两个:

  1. 页面元素由AngularJS动态渲染,代码执行时元素还未加载完成,直接调用FindElement会抛出找不到元素的异常
  2. 你定位的li[ng-if='item.end_time']元素本身没有直接文本,实际时间文本在该li的子节点中

修正方案

1. 添加显式等待

用Selenium的WebDriverWait等待元素加载完成,规避页面渲染延迟的问题。

2. 调整元素定位方式

通过浏览器开发者工具查看页面结构,定位到承载时间文本的子元素,提升定位精准度。

修正后的代码

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;

namespace WebScraperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.bidrl.com/auction/autotool-auction-1525-c-street-marysville-ca-september-19th-134834/item/wheel-cover-17686623/");

            // 获取标题(原代码保留)
            String text1 = driver.FindElement(By.CssSelector("span[ng-bind-html='item.title']")).Text;
            System.Diagnostics.Debug.WriteLine(text1);

            // 显式等待竞价结束时间元素加载,最多等待10秒
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            IWebElement endTimeElement = wait.Until(d => d.FindElement(By.CssSelector("li[ng-if='item.end_time'] span")));
            
            // 获取时间文本
            String text2 = endTimeElement.Text;
            System.Diagnostics.Debug.WriteLine(text2);

            driver.Quit();
        }
    }
}

补充说明

  • 如果上述定位仍无效,可重新通过浏览器F12开发者工具查看Bidding Ends对应的HTML结构,调整CSS选择器(比如改用类名定位或更完整的元素路径)。
  • 操作完成后务必调用driver.Quit()关闭浏览器,避免资源泄漏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 20:40:23