Selenium C#中XPath定位li元素错误,无法获取赛事计时器值
Hey there! I see you're just getting started with Selenium and ran into two common issues—let's break them down and fix them together.
Your suspicion is spot-on—li[''] is invalid XPath syntax. The brackets require a valid condition (like a position, attribute check, or text match), not an empty string. On top of that, your absolute XPath is extremely long and brittle (any tiny change to the page structure will break it). Let's simplify and make it more reliable.
Solutions:
- Remove the invalid
li['']segment: If you need to target a specific<li>underul[@class='events--list'], use a valid condition likeli[1](for the first list item) or justlito match all list items under that container. - Use a shorter, relative XPath: Since the target div has a unique class
event--timer, we can target it directly for a much more stable locator.
Here's your corrected code:
var driver = new ChromeDriver(); driver.Navigate().GoToUrl("https://www.favorit.com.ua/uk/live/"); // Option 1: Simplified, reliable relative XPath (recommended) string myTime = driver.FindElement(By.XPath("//div[@class='event--timer']")).GetAttribute("innerHTML"); // Option 2: Fixed version of your original absolute XPath // string myTime = driver.FindElement(By.XPath("/html/body/div[@class='wrapper']/" + // "div[@class='contentdiv']/" + // "div[@id='middle']/" + // "div[@id='container']/" + // "div[@id='content']/" + // "div[@class='content clear-block bet_a_c']/" + // "div[@id='livediv']/" + // "div[@class='selected--sport--block']/" + // "div[@class='view-wrapper']/" + // "ul[@class='sport--list']/" + // "li[@class='sport--block']/" + // "ul[@class='category--list--block']/" + // "li[@class='category--block sp_1']/" + // "ul[@class='events--list']/" + // "li/" + // Removed invalid li[''] // "ul[@class='event--head-block']/" + // "div[@class='event--head']/" + // "div[@class='event--short--info']/" + // "div[@class='time--block']/" + // "div[@class='headerdiv']/" + // "div[@class='event--timer']")).GetAttribute("innerHTML"); Console.WriteLine(myTime); Console.ReadLine(); driver.Quit(); // Don't forget to clean up the browser process!
The most common cause of Chrome launching twice is that your code is instantiating ChromeDriver more than once. Here's what to check and fix:
Possible Causes:
- Duplicate
new ChromeDriver()calls in your codebase (maybe in another method or section of your script). - If using a testing framework (like NUnit/MSTest), accidental initialization in both a setup method and your test method.
- Leftover driver processes from previous debug runs (your IDE might not have cleaned them up properly).
Solutions:
- Ensure only one driver initialization: Double-check your entire script to confirm there's only one
new ChromeDriver()line. - Use a
usingstatement: This automatically disposes of the driver when done, preventing leftover processes and accidental multiple launches:using (var driver = new ChromeDriver()) { driver.Navigate().GoToUrl("https://www.favorit.com.ua/uk/live/"); string myTime = driver.FindElement(By.XPath("//div[@class='event--timer']")).GetAttribute("innerHTML"); Console.WriteLine(myTime); Console.ReadLine(); } // Driver quits and cleans up automatically here - Clean up debug sessions: Before starting a new run, make sure you stop any previous debug sessions completely to kill leftover browser processes.
Hope these fixes get you back on track with Selenium! Let me know if you run into any other snags as you learn.
内容的提问来源于stack exchange,提问作者The Vee

