1

I have created a custom method to implicitly wait for an element to load, then I use this in a custom click method as so:

public static void WeElementToBeClickable(this IWebElement element, int sec = 10)
{
    var wait = new WebDriverWait(Driver.Browser(), TimeSpan.FromSeconds(sec));
    wait.Until(ExpectedConditions.ElementToBeClickable(element));     
}

public static void WeClick(this IWebElement element, int sec = 10)
{
    element.WeElementToBeClickable();
    element.Click();
}

I then attach this to any element I click on to make sure it always polls the DOM to make sure the element has been loaded, but it doesn't seem to wait for some specific elements to load.

I'm searching for this element as so:

<span class="button add fr" onclick="GoToHash('/ContractorCommon/Contractor/ContractorAdd', null, 'Contractor'); "><span class="icon-add"></span> Contractor</span>

public IWebElement AddContractorIcon => Driver.FindElement(By.XPath("//span[@class='button add fr']"));

But it always gives off the following exception straight away:

OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='button add fr']"}

I've tried everything to get it to wait for this element to load but I can't seem to figure it out. Weirdly enough if I debug it...it finds the element.

Any help would be greatly appreciated!

bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
  • Have you tried Thread.Sleep() ? – Maksym Labutin Nov 21 '18 at 15:40
  • @MaksymLabutin yes I've tried and it works with a Thread.Sleep() but for obvious reasons I want to try and avoid using it. Plus I'm quite sure this ImplicitWait should be working! – L Sainsbury Nov 21 '18 at 15:51
  • Maybe the span (button) has a fade in effect? – Maksym Labutin Nov 21 '18 at 16:09
  • @MaksymLabutin I know that the button is dynamic as once you've clicked on the button it turns into a "Save" button but my ImplicitWait should still poll the DOM until it's present shouldn't it? – L Sainsbury Nov 21 '18 at 16:22
  • Try `ExpectedConditions.VisibilityOfAllElementsLocatedBy` https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_VisibilityOfAllElementsLocatedBy_1.htm – Maksym Labutin Nov 21 '18 at 16:40
  • @MaksymLabutin I've managed to get it working using the proposed solution, however there's quite a lot of methods I've generated that would need this code. I want to abstract it back into one of my base pages so I can just call it but I'm having a little trouble as it's looking for a string.`public static void WeVisibilityOfElementById(this IWebElement element) { WebDriverWait wait = new WebDriverWait(Driver.Browser(), TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id(element.ToString))); }` – L Sainsbury Nov 21 '18 at 20:16
  • Ah, I was missing the parenthesis for ToString() but it doesn't seem to work anyway! :) – L Sainsbury Nov 21 '18 at 20:22
  • @Sainsbury, Try instead `(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id(element.ToString)))` this `(ExpectedConditions.ElementToBeClickable(element))` – Maksym Labutin Nov 22 '18 at 08:31
  • @MaksymLabutin we can't use 'this' keyword in a static method unfortunately! My Class is also static so I'm unable to change it. – L Sainsbury Nov 22 '18 at 09:10
  • I mean you need replace to this piece of code (without "this")(ExpectedConditions.ElementToBeClickable(element)) – Maksym Labutin Nov 22 '18 at 09:11

0 Answers0