Alex M
Alex M

Reputation: 3

C# Selenium - OpenQA.Selenium.ElementNotInteractableException - Element could not be scrolled into view

I am currently trying to click an element on a webpage using selenium with C#. I need to click a Div element based on a child element that has the text 'Test App'.

Here is the HTML snippet for the object;

<div class="application_items">
    <a href="www.google.com" target="_self">
        <div class="homePageItems">
           <div class="small" style="display: block;">
               <div class="AppLabel">Test App</div>
           </div>
           <div class="big" style="display: none;">
               <div class="AppLabel">Test App</div>
                   <div class="underTitle"></div>
           </div>
       </div>
   </a></div>

And here is my C# code to try access the first appearance of the app name 'Test App';

Element(By.XPath("//div[@class='small']/*[text()[contains(., 'Test App')]]"));

When this runs, I get an error;

OpenQA.Selenium.ElementNotInteractableException: 'Element <div class="AppLabel"> could not be scrolled into view'

I thought the error may be because the program was accessing the second occurence of 'App Test', so I have tried setting the div with the class 'big' visible (display: block;) with the following code, but it doesn't seem to help;

IWebElement elem = Driver.FindElement(By.XPath("//div[@class='small']/div[text()[contains(., 'Test App')]]"));

String js = "arguments[0].style.height='auto'; arguments[0].style.display='block';";

((IJavaScriptExecutor)Driver).ExecuteScript(js, elem);

I apologise if something similar to this has been asked before, I spent a while browsing similar subjects but didn't find what I needed. If anyone could please point me in the right direction, that would be much appreciated.

Upvotes: 0

Views: 2120

Answers (4)

satya
satya

Reputation: 1

IReadOnlyCollection<IWebElement> elems = Driver.FindElements(By.XPath("//div[.='Test App']")).Where(e => e.Displayed).ToList();
elems.ElementAt(0).Click();

I am using this its perfectly working for me

Upvotes: 0

iamsankalp89
iamsankalp89

Reputation: 4739

You can siply use by using classname, Correct the code as I dont know the C sharp very well

driver.FindElement(By.ClassName("AppLabel")).Click();

Also you can use xpath

driver.FindElement(By.XPath(“//div[@class='small']/div[text()='Test App'])).Click();

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

As per the HTML you have provided to click on the first appearance of the app with text as Test App you can use either of the following line of code :

  • CssSelector :

    Element(By.CssSelector("div.homePageItems > div.small > div.AppLabel"));
    
  • XPath :

    Element(By.XPath("//div[@class='homePageItems']/div[@class='small']/div[@class='AppLabel' and contains(., 'Test App')]"));
    

Upvotes: 0

JeffC
JeffC

Reputation: 25611

You could get all the elements, filter them down to only the visible ones, and then click the first (or whatever) one.

IReadOnlyCollection<IWebElement> elems = Driver.FindElements(By.XPath("//div[.='Test App']")).Where(e => e.Displayed).ToList();
elems.ElementAt(0).Click();

It's also possible that you need to add a wait to make sure that part of the page has loaded.

Upvotes: 1

Related Questions