0

I an working on some unit testing using Selenium with Nunit in c#.

In theory this is quite simple, i have a element i want to click to go to another page. The issue i have is that the part that makes it clickable loads later than the actual element. Meaning that if i just wait until the element has loaded, clicking it still does nothing. I also tried waiting for it to be clickable, but it still doesn't seem to work.

Is there any way around this except a static delay?

Thanks

Jaco9307
  • 13
  • 7
  • Is there some part of the page that needs to finish loading before the element is clickable? You could wait for that part to be visible and then try clicking. Have you tried waiting for background processes to finish like jQuery, etc.? You've have to investigate what's running or talk to dev if you own the site. – JeffC Oct 08 '19 at 18:01

2 Answers2

2

I suggest that you create a method that waits for ajax/jquery:

public void WaitForJqueryAjax() {
    int delay = MaxdelaySeconds;
    while(delay > 0) {
        Thread.Sleep(1000);
        var jquery = (bool)(this.driver as IJavaScriptExecutor)
            .ExecuteScript("return window.jQuery == undefined");
        if(jquery) {
            break;
        }
        var ajaxIsComplete = (bool)(this.driver as IJavaScriptExecutor)
            .ExecuteScript("return window.jQuery.active == 0");
        if(ajaxIsComplete) {
            break;
        }
        delay--;
    }
}

In my implementation, int delay = MaxdelaySeconds; has the same value as my drivers implicit wait time.

(If needed, modify the above method to take your driver as parameter)

Agent Shoulder
  • 586
  • 8
  • 22
  • Tried this, but it seems to change nothing. I still get the failed clicks and similar errors – Jaco9307 Oct 25 '19 at 10:10
  • @Jaco9307 What type of exception(s) do you get? – Agent Shoulder Oct 28 '19 at 09:40
  • Sorry about the late reply. The main issue is that i dont even get any exceptions. The code just clicks before the element is actually clickable and nothing happens. The code just continues on not even knowing the click didnt do anything, until it fails to find a element, which should have shown up after changing the page. – Jaco9307 Nov 01 '19 at 08:56
  • @Jaco9307 What is it that makes it clickable? The behavior you are experiencing is by design in Selenium. As you say, even if you wait for the element to appear, it is not clickable, hence you should wait for the event that makes it clickable... You could try to wait for the element to be clickable, as described in https://stackoverflow.com/questions/16057031/webdriver-how-to-wait-until-the-element-is-clickable-in-webdriver-c-sharp/35883458 – Agent Shoulder Nov 07 '19 at 10:21
  • +1, in my case, a table is filled by clicking a button, so I did `WaitForJqueryAjax` after clicking and the table is filled correctly in 5 second. – M.Hassan Nov 25 '20 at 13:22
-2

Maybe you need to execute a JavaScript or Jquery code

For example :

            driver.ExecuteJavaScript("$(\"#CategorylId\").CascadingDropDown(\"#WareId\", \'/store/log/AsyncWare\');");
            WaitUntilLoad(driver, "WareId", 5000);
            page = driver.PageSource;

For additional information this link maybe useful

Amir Azad
  • 113
  • 5
  • This answer seems to be an answer to a different question??? I'm unsure how this answer the question here. – JeffC Oct 08 '19 at 17:57
  • this is an example not my answer **Maybe you need to execute a JavaScript or Jquery code** – Amir Azad Oct 09 '19 at 04:24
  • How does your example JS relate to the question? The question is asking about how to wait until an element is clickable and your JS does something unrelated to this. Then you reference `WaitUntilLoad()` which is not defined here. Then finally you pull the page source... which again has nothing to do with the question. – JeffC Oct 09 '19 at 04:33