[英]Selenium C# error: ElementNotIteractableException even though it is DOM clickable
I have a very basic Selenium test for bing.com on Chrome using C# .NET Core 3.1.我在 Chrome 上使用 C# Z303CB0EF9EDB9082D61BBBE5825 核心对 bing.com 进行了非常基本的 Selenium 测试。 It goes like this
它是这样的
[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();
}
Problem is, I keep getting a exception for the last line (the click step)问题是,我不断收到最后一行的异常(点击步骤)
Message: Test method SeleniumDemo.MySeleniumTests.TheBingSearchTest2 threw exception: OpenQA.Selenium.ElementNotInteractableException: element not interactable (Session info: chrome=105.0.5195.53)
消息:测试方法 SeleniumDemo.MySeleniumTests.TheBingSearchTest2 抛出异常:OpenQA.Selenium.ElementNotInteractableException:元素不可交互(会话信息:chrome=105.0.5195.53)
Stack Trace: WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) WebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters) WebElement.Execute(String commandToExecute, Dictionary`2 parameters) WebElement.Click() MySeleniumTests.TheBingSearchTest2() line 59
堆栈跟踪:WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) WebDriver.Execute(String driverCommandToExecute, Dictionary`2 参数) WebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 参数) WebElement.Execute(String commandToExecute, Dictionary`2 参数) WebElement .Click() MySeleniumTests.TheBingSearchTest2() 第 59 行
The logged output is correct, it gives记录的 output 是正确的,它给出
found element <input id="sb_form_go" type="submit" aria-label="Search" name="search" value="" tabindex="0">
When doing good ol' javascript fiddling, when I get the element #sb_form_go, and invoke.checkVisiblity() it returns true.当做得好 ol' javascript 摆弄时,当我得到元素#sb_form_go 和invoke.checkVisiblity() 它返回true。 I can even do.click() on it and it works.
我什至可以对其执行.click() 并且它可以工作。
I tried adding wait time before the last line, converting the method to async and doing我尝试在最后一行之前添加等待时间,将方法转换为异步并执行
await Task.Delay(10000);
doesn't work.不起作用。 I tried to do
我试着做
var wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait1.Until(ExpectedConditions.ElementToBeClickable(By.Id("sb_form_go")));
but then the error becomes但随后错误变为
OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds
OpenQA.Selenium.WebDriverTimeoutException:10 秒后超时
Which is weird coz I'm sure it IS visible这很奇怪,因为我确定它是可见的
Any idea here?这里有什么想法吗? This is the code to replicate the problem (not my exact private code but so line numbers are different) https://github.com/koko983/SeleniumBingDemo
这是复制问题的代码(不是我确切的私人代码,但行号不同) https://github.com/koko983/SeleniumBingDemo
it seems you are trying to clock in the <input>
element, which is probably the element for which is attached the onClick
event.您似乎正在尝试
<input>
元素,这可能是为其附加了onClick
事件的元素。 In this case - the there is an <label>
element which should be the "wrapper" for the input element.在这种情况下 - 有一个
<label>
元素,它应该是输入元素的“包装器”。
You have to click on the label element to trigger the actual click for search icon.您必须单击 label 元素才能触发实际单击搜索图标。
I would suggest, also to start using wait
functions, as a safe-wait for the UI to stabilize before doing an interaction.我建议,也开始使用
wait
功能,作为安全等待 UI 在进行交互之前稳定下来。
Example:例子:
var appURL = "http://www.bing.com/";
driver.Navigate().GoToUrl(appURL);
driver.Manage().Window.Maximize();
// typing inside search box.
driver.FindElement(By.Id("sb_form_q")).SendKeys("Azure Pipelines");
// click on the search icon.
driver.FindElement(By.Id("search_icon")).Click();
Just try clicking on the parent element:只需尝试单击父元素:
non_clickable_element.FindElement(By.XPath("..")).click();
Or, try clicking on the first child of the element:或者,尝试单击元素的第一个子元素:
non_clickable_element.FindElements(By.XPath(".//*"))[0].click()
Most often when you can't click on items you need to click on the parent or on the child, even if the element you are trying click is visible.大多数情况下,当您无法单击需要单击父项或子项的项目时,即使您尝试单击的元素可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.