简体   繁体   English

Selenium C# 错误:即使它是 DOM 可点击的 ElementNotIteractableException

[英]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.

相关问题 元素不可点击硒C# - Element not clickable selenium c# 即使文件夹存在于 c# 中,也找不到文件夹错误 - Folder not found error even though the folder exists in c# 即使在 C# 接口内部有方法访问错误 class - Access error even though there is a method inside the C# Interface class Selenium / C#Xpath无效,尽管可以在DOM中搜索 - Selenium /C# Xpath doesn't work though can be searched in the DOM Selenium WebDriver C#:即使元素被禁用,Element.Enabled 也会返回 True - Selenium WebDriver C#: Element.Enabled returns True even though element is disabled 即使存在C#硒,也无法在页面上找到此元素 - Can't find this element on page even though it is exist C# selenium 为什么 Selenium 无法读取下拉文本,即使它们存在于 HTML 中。 (C#, VS2019, 硒) - Why Selenium can't read texts of dropdown even though those exist in HTML. (C#, VS2019, Selenium) 即使应该是C#中的无效json - Not valid json in c# even though it should be 即使我尊重点运算符优先级(C#),也会出现“预期标识符”错误 - Casting giving 'identifier expected' error even though I am respecting dot operator precedence (C#) 编译错误:Window 不包含模板的定义,即使定义了模板。 WPF C# - Compile Error: Window does not contain definition for template, even though template is defined. WPF C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM