简体   繁体   中英

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. 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)

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

The logged output is correct, it gives

    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. I can even do.click() on it and it works.

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

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

it seems you are trying to clock in the <input> element, which is probably the element for which is attached the onClick event. In this case - the there is an <label> element which should be the "wrapper" for the input element.

You have to click on the label element to trigger the actual click for search icon.

I would suggest, also to start using wait functions, as a safe-wait for the UI to stabilize before doing an interaction.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM