如何使用Selenium和C#点击submit类型按钮?遇拦截异常求助
Hey there, let's sort out this click intercept issue you're hitting! The problem is clear: that <i> element is sitting on top of your submit button, so Selenium can't physically "click" the button through it. Here are a few practical fixes you can try right away:
1. Wait for the button to be fully clickable (and let the blocker disappear)
Sometimes that blocking element is a temporary tooltip, loading icon, or overlay that just needs a moment to vanish. Use WebDriverWait to wait until the button is ready to be interacted with—this will automatically handle short-lived overlays:
using OpenQA.Selenium.Support.UI; using OpenQA.Selenium; // Set up a 10-second wait window WebDriverWait wait = new WebDriverWait(chrome[row_index], TimeSpan.FromSeconds(10)); // Wait until the button is clickable, then trigger the click IWebElement submitButton = wait.Until(ExpectedConditions.ElementToBeClickable( By.XPath("//*[@id=\"app\"]/div/div[1]/div[2]/div[3]/div[2]/div/button") )); submitButton.Click();
2. Bypass the UI with a JavaScript click
If the blocking element won't go away (like a fixed persistent overlay), you can directly trigger the button's click event using JavaScript. This works because JavaScript doesn't care about element overlap—it just fires the event regardless:
IWebElement submitButton = chrome[row_index].FindElement( By.XPath("//*[@id=\"app\"]/div/div[1]/div[2]/div[3]/div[2]/div/button") ); IJavaScriptExecutor js = (IJavaScriptExecutor)chrome[row_index]; js.ExecuteScript("arguments[0].click();", submitButton);
3. Dismiss the blocking element first
If that <i> icon is an interactive element (like a dismissible tooltip), you can click it to get it out of the way before accessing the button:
WebDriverWait wait = new WebDriverWait(chrome[row_index], TimeSpan.FromSeconds(10)); // Wait for the blocking icon to load, then click to dismiss it IWebElement blocker = wait.Until(ExpectedConditions.ElementIsVisible( By.XPath("//i[@class='font-20 d-block mb-1 icon-question']") )); blocker.Click(); // Now you can click the submit button normally IWebElement submitButton = chrome[row_index].FindElement( By.XPath("//*[@id=\"app\"]/div/div[1]/div[2]/div[3]/div[2]/div/button") ); submitButton.Click();
Try these approaches one by one—usually the JavaScript click or wait strategy resolves this kind of intercept issue quickly!
内容的提问来源于stack exchange,提问作者Lộc Nguyễn Tấn

