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

Selenium C#调用Click()报ElementNotInteractableException问题求助

解决Selenium点击Bing搜索按钮时的ElementNotInteractableException问题

问题背景

我使用C# .NET Core 3.1在Chrome浏览器上编写了针对bing.com的基础Selenium测试,代码如下:

[TestMethod]
[TestCategory("Chrome")]
public void TheBingSearchTest2()
{
    var appURL = "http://www.bing.com/";
    driver.Navigate().GoToUrl(appURL);
    driver.Manage().Window.Maximize();
    driver.FindElement(By.Id("sb_form_q")).SendKeys("Azure Pipelines");
    var koss = driver.FindElement(By.Id("sb_form_go"));
    Console.WriteLine("found element {0}", koss.GetAttribute("outerHTML"));
    koss.Click();
}

执行最后一行Click()时,持续抛出异常:

Message: Test method SeleniumDemo.MySeleniumTests.TheBingSearchTest2 threw exception: OpenQA.Selenium.ElementNotInteractableException: element not interactable (Session info: chrome=105.0.5195.53)

Stack Trace: WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) WebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) WebDriver.InternalExecute(String driverCommandToExecute, Dictionary2 parameters) WebElement.Execute(String commandToExecute, Dictionary`2 parameters) WebElement.Click() MySeleniumTests.TheBingSearchTest2() line 59

日志显示已正确找到目标元素:

found element <input id="sb_form_go" type="submit" aria-label="Search" name="search" value="" tabindex="0">

通过JavaScript操作该元素时,调用checkVisiblity()返回true,且click()可正常执行。尝试在Click()前添加固定等待(await Task.Delay(10000))无效;使用显式等待:

var wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait1.Until(ExpectedConditions.ElementToBeClickable(By.Id("sb_form_go")));

则抛出超时异常:

OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds

确认元素可见,求解决方法。

解决方案

1. 使用更精确的元素定位

Bing页面DOM中可能存在多个id为sb_form_go的元素(比如隐藏的备用元素),导致你定位到的是不可交互的实例。可以结合父元素缩小定位范围,比如指定搜索表单内的提交按钮:

var searchButton = driver.FindElement(By.CssSelector("form#sb_form input#sb_form_go"));
searchButton.Click();

2. 改用JavaScript执行点击

既然测试证明JavaScript可以正常触发点击,直接用JS替代Selenium原生点击:

IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)driver;
var searchButton = driver.FindElement(By.Id("sb_form_go"));
jsExecutor.ExecuteScript("arguments[0].click();", searchButton);

3. 先滚动到元素视口再点击

部分情况下元素虽可见但不在浏览器视口内,Selenium会判定为不可交互。先滚动到元素位置,再等待可点击后执行点击:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var searchButton = driver.FindElement(By.Id("sb_form_go"));

// 滚动到元素位置
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView({block: 'center'});", searchButton);

// 等待元素可点击
wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return arguments[0].checkVisibility();", searchButton));

searchButton.Click();

额外检查项

  • 确保ChromeDriver版本与Chrome浏览器版本完全匹配(当前Chrome为105.x,需使用对应版本的ChromeDriver);
  • 确认已安装Selenium.SupportNuGet包,ExpectedConditions类依赖该包;
  • 检查页面是否有动态加载的遮罩层,可能遮挡了按钮(可通过浏览器开发者工具的元素审查确认)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:01:06