1

I used code:-

public Boolean RetryingFindClick(By by)
    {
        Boolean result = false;
        int attempts = 0;
        while (attempts < 4)
        {
            try
            {

                BrowserHelper.WebDriver.FindElement(by).Click();
                result = true;
                break;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            attempts++;
        }
        return result;
    }

But i have been told that this is not a best practice and many times it does not work.

HTML:

<div _ngcontent-c20="" class="col-lg-12 mb-3">
    <ng-select _ngcontent-c20="" id="UploaderName" class="ng-pristine ng-valid ng-touched">
        <!---->
        <div class="open below" tabindex="0">
            <!---->
            <div class="single">
                <!---->
                <!---->
                <div class="placeholder"> Uploader Name </div>
                <!---->
                <!---->
                <div class="toggle"> ▲ </div>
                <!---->
            </div>
            <!---->
        </div>
        <!---->
        <select-dropdown>
            <div class="below" style="width: 723px; top: 32px; left: 0px;">
                <!---->
                <div class="filter">
                    <input autocomplete="off" placeholder="">
                </div>
                <div class="options">
                    <ul>
                        <!---->
                        <!---->
                        <li class="message"> No results found </li>
                    </ul>
                </div>
            </div>
        </select-dropdown>
Subburaj
  • 2,294
  • 3
  • 20
  • 37
Mahak Malik
  • 165
  • 2
  • 11
  • i dont know why the post got downgraded.I have done a lot of research from using Thread.sleep to wait.untill. Wait.untill does not seam to have a method to detect the DOM attachment of the element. It is a preety usefull question for newbies. – Mahak Malik Jul 27 '18 at 15:51
  • Can you share which element is causing stale element reference , in your code? – cruisepandey Jul 27 '18 at 16:20
  • String org = StringRandomGen.generateRandomString(); R_Org.SendKeys(org); RetryingFindClick(By.XPath("//* [@id=\"Organization\"]/select- dropdown/div/div/ul/li")); – Mahak Malik Jul 27 '18 at 16:52

1 Answers1

3

You can use the explicit wait condition in order to find the element as below.It will be waited until the element is clickable for the given time.If the element is not attached to the DOM/not clickable within the specified time, then it will throw TimeoutException. So, you can specify the required time in wait condition.

NuGet Package :

DotNetSeleniumExtras.WaitHelpers

Code:

public Boolean RetryingFindClick(By by){

    try
    {
        var  wait = new WebDriverWait(BrowserHelper.WebDriver, TimeSpan.FromSeconds(20));
        wait.Until(ExpectedConditions.ElementToBeClickable(by))    
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        return false;//Element is not found until the specified time
    }
    return true;
}

Edit:

Please add the click action in your test method (removed from RetryingFindClick method)

BrowserHelper.WebDriver.FindElement(by).Click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Subburaj
  • 2,294
  • 3
  • 20
  • 37
  • i used this does not work:- var wait = new WebDriverWait(BrowserHelper.WebDriver,new TimeSpan(0,0,40)); wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"UserType\"]/select-dropdown/div/div[2]/ul/li"))); – Mahak Malik Jul 27 '18 at 16:50
  • Can you add the html? – Subburaj Jul 27 '18 at 16:55
  • Uploader Name
    – Mahak Malik Jul 27 '18 at 17:00
  • 1
    sorry i dont know how to edit code or write in stackoverfow – Mahak Malik Jul 27 '18 at 17:00
  • Based on your xpath, some html tags are missing. can you please share the html after select-dropdown tag as well? – Subburaj Jul 27 '18 at 17:03
  • understand what you are asking for but it is a third party code and we are referencing..so i do not have the code – Mahak Malik Jul 27 '18 at 17:05
  • select-dropdown>
    • No results found
    – Mahak Malik Jul 27 '18 at 17:05
  • Can you change the xpath as //select-dropdown//ul/li and then please try – Subburaj Jul 27 '18 at 17:08
  • It looks, here no result is found is displaying in li tag.Whether it will have any clickable element based on certain condition? – Subburaj Jul 27 '18 at 17:10
  • You need to edit your question and add this HTML since it will probably be useful to others trying to answer the question. – JeffC Jul 27 '18 at 17:49
  • 1
    With the wait, I don't think you want to actually attempt a click of the element... just return a boolean value. – JeffC Jul 27 '18 at 17:50
  • the problem is all together something else.As the website is JS heavy that is why the clicking of elements through webdriver is behaving such a way:- Further discussion https://stackoverflow.com/questions/34562061/webdriver-click-vs-javascript-click#34796379 – Mahak Malik Jul 27 '18 at 19:02