0

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you so much. I used this and also added a wait, and now everything works perfectly.
0

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')]"));
    

1 Comment

Thank you for your comment, but unfortunately I still get an error. I ended up using the code suggested by JeffC. Perhaps this may help you in the future as well.
0

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();

1 Comment

Thank you, this is a perfect solution for the problem I described. However, this HTML block is replicated for many apps - I need to code something that can select an app based from user input (I did not mention this, I apologise).
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.