使用C#版Selenium获取网页JavaScript变量的方法咨询
Extracting the
series JavaScript Array with Selenium in C# Got it, let's break down exactly how to grab that series array from your target page using Selenium in C#. Here's a step-by-step guide plus a full code example:
1. Set Up Your Dependencies
First, you'll need to install the necessary NuGet packages in your C# project:
Selenium.WebDriver: Core Selenium library for browser automationSelenium.WebDriver.ChromeDriver: Chrome browser driver (swap for Edge/Firefox equivalents if you prefer those browsers)Newtonsoft.Json: To convert the raw JavaScript array into usable C# objects (optional but makes data manipulation way easier)
You can install these via the NuGet Package Manager or run these commands in the Package Manager Console:
Install-Package Selenium.WebDriver Install-Package Selenium.WebDriver.ChromeDriver Install-Package Newtonsoft.Json
2. Full Implementation Code
The core trick here is using Selenium's IJavaScriptExecutor interface to run a script that pulls the series variable directly from the page's global scope. Here's the complete code:
using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using Newtonsoft.Json.Linq; using OpenQA.Selenium.Support.UI; class SeriesExtractor { static void Main() { // Configure Chrome to run in headless mode (no visible browser window) var chromeOptions = new ChromeOptions(); chromeOptions.AddArgument("--headless=new"); chromeOptions.AddArgument("--disable-gpu"); // Initialize the Chrome driver (wrap in 'using' to auto-dispose resources) using var driver = new ChromeDriver(chromeOptions); // Navigate to the actual page (ignore the view-source: prefix — that's just browser's source view) string targetUrl = "https://school2school.net/stations/?siteCode=TA00279&variableCode=temperature"; driver.Navigate().GoToUrl(targetUrl); // Wait until the `series` variable is loaded (avoids race conditions) var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(driver => (bool)((IJavaScriptExecutor)driver).ExecuteScript("return typeof series !== 'undefined';")); // Extract the series array via JavaScript execution var jsExecutor = (IJavaScriptExecutor)driver; var rawSeriesData = jsExecutor.ExecuteScript("return series;"); // Convert the raw object to a JArray for easy parsing var seriesArray = JArray.FromObject(rawSeriesData); // Example: Print out each item in the array Console.WriteLine("Extracted series array data:"); foreach (var dataPoint in seriesArray) { Console.WriteLine(dataPoint.ToString()); // If you know the structure of each data point, map it to a custom class: // var mappedData = dataPoint.ToObject<YourCustomDataClass>(); } } }
3. Key Tips & Troubleshooting
- Wait for the variable: Always use
WebDriverWaitto ensure theseriesvariable exists before trying to extract it. This prevents errors if the page takes a moment to load the JavaScript. - Variable scope check: If
seriesisn't in the global scope (e.g., it's locked inside a function or closure), you'll need to adjust the JavaScript script to access it. For example, if it's inside awindow.onloadcallback, you might need to expose it globally or modify the script to grab it from its local context. - Browser flexibility: If you prefer Edge or Firefox, replace
ChromeDriverwithEdgeDriverorFirefoxDriverand install the corresponding NuGet packages (likeSelenium.WebDriver.EdgeDriver).
内容的提问来源于stack exchange,提问作者Manzurul Hoque Rumi
相关产品推荐
相关产品推荐

