1

TestInChrome1 throws me an exception - "OpenQA.Selenium.ElementNotInteractableException: element not interactable" However, when JavaScriptExecutor is used in TestInChrome2, it works well.

My questions are:

  1. why does Click() method is not working in TestInChrome1?

  2. how do can we determine that JavaScriptExecutor is necessary without trial and errors?

    [TestMethod]
    public void TestInChrome1()
    {
        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://ultimateqa.com/");
        IWebElement element = driver.FindElement(By.TagName("title"));
        element.Click();
        driver.Quit();
    }
    
    
    
    
    [TestMethod]
    public void TestInChrome2()
    {
        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://ultimateqa.com/");
        IWebElement element = driver.FindElement(By.TagName("title"));
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        string title = (string)js.ExecuteScript("return document.title");
        driver.Quit();
    }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Matthew Lee
  • 23
  • 1
  • 8
  • You can't click the `` element, and the java script just returning it and not trying clicking it. – Guy Dec 11 '19 at 06:36
  • You seem to be testing, In that case you should never use JSExecutor. Instead you should adjust your test or fix your UI – pguardiario Dec 11 '19 at 07:23
  • You can compare JavascriptExecutor#ExecuteScript with typing javascript into the console of the browser under test, rather than using the WebDriver interface. I'm not familiar with C# bindings, but I bet there is something like a `driver.Title` – Len Dec 11 '19 at 09:46

2 Answers2

1

The <title> tag is required in all HTML documents and it defines the title of the document. The element:

  • Defines a title in the browser toolbar.
  • Provides a title for the page when it is added to favorites.
  • Displays a title for the page in search-engine results.

Note A: You can NOT have more than one element in an HTML document.

Note B: If you omit the <title> tag, the document will not validate as HTML.

If you observe the HTML DOM of any website e.g. https://ultimateqa.com/ you will observe the <title> resides within the <head>. Hence the information of this tag is visible and readable but not interactable.

page_title


TestInChrome1()

So as per the discussion above, in TestInChrome1():

  • You won't be able to invoke Click() on the title tag.

  • To extract the title you can use the Title property from IWebDriver Interface and you can use the following solution:

      Console.WriteLine(driver.Title);
    

TestInChrome2()

By now you must be aware that Selenium beside allowing users to simulate common activities performed by end-users, entering text into fields, selecting drop-down values and checking boxes, and clicking links in documents, it also provides many other controls such as arbitrary JavaScript execution. To extract the <title> you can use the ExecuteScript() method from IJavaScriptExecutor interface as follows:

Console.WriteLine((string)((IJavaScriptExecutor)driver).ExecuteScript("return document.title"));
Community
  • 1
  • 1
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

When we use selenium, it has methods written in such a way that it tries to emulate how user will interact with the webpage.

So, lets take the case there is a button on the screen but it never displays on the visible area of the screen. Maybe it is not scrollable (or maybe it is always hidden) and will be never be available to user. Now this is a valid bug.

  • If you use javascript executor, it will click on the buttob and your script won't be able to catch this issue
  • If you use selenium method for click, script will fail giving you some exception

In my case, I have encountered Element not interactable exception in mostly false positive (invalid, non-bug) scenarios

  • if some field on screen was inactive earlier, you performed some action it became active. but due to faster execution, it was still inactive, when script interacted with it
  • Say you have dropdown on screen, you expand the dropdown and click on some field. Now you click some other field when dropdown was closed. While execution dropdown does not close immediately and element you want to access next is obsecured by that dropdown (can happen for popups, or some element that expands on screen, combobox, searchbox)

If you see too many issues due to element not interactable , just catch this exception, take screenshot for your reference, generate a warning in logs for yourself and you can implement direct click using javascript executor in catch block. Atleast by generating warning for yourself, you can check if you are not missing out on an actual issue.

Hope this helps.

Ishan
  • 211
  • 1
  • 8