5

How can I wait for element to be clickable in Selenium WebDriver?

When I execute my tests in Firefox or Internet Explorer I have no problems, but when I try to run it on Chrome, I get an element is not clickable error when I try to click on the submit button. I tried with verifying that the element is there and it is visible and enabled.

Please give your answers in C#.

  • Did you try to search how would you implement a custom wait in C# ? Take a look at this question and try to rebuild it with your own solution/version stackoverflow.com/questions/21339339/… – demouser123 Aug 3 '15 at 16:19
  • I already tried with checking if the element is visible or enabled and it did not work. There is no change in the code for the button so i can not check for difference in the class or something like that. – MSeykov Aug 4 '15 at 8:47
5

Below code should help you.

public static class WebDriverExtensions
{
    public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => drv.FindElement(by));
        }
        return driver.FindElement(by);
    }
}
  • 1
    why does this work, would you please explain? – Malachi Aug 3 '18 at 20:43
2

I have been using this line of code when I first create my browser instance

[Given(@"I have opened an IE browser")]
public void GivenIHaveOpenedAnIEBrowser()
{

    Ie = new InternetExplorerDriver();
    Ie.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(FiveSeconds));
}

it's actually the second line in this method, I have a couple of different times factored into constants because I had the same issue in IE. most of my issues were caused by overlapping tests, but there were quite a few times where the DOM wasn't loaded completely yet.

This really helped, I cannot remember where I found this solution, it was somewhere on SO.

1

This may caused by any web elements which is overlapping with submit button. For an example, you have clicked on a dropdown and item lists are long so it may over lap the submit button. FireFox and IE can click on the submit button in this scenario but chrome can not. You can scroll to the submit button and click on it. you can use


wait.Until(ExpectedConditions.ElementToBeClickable(By locator));

  • Thank you for the answer, but when i try to use 'ExpectedConditions.ElementToBeClickable' it says that 'Expected Conditions' does not contain a definition for 'ElementToBeClickable'. Am i missing any 'Using' or something? – MSeykov Aug 3 '15 at 9:49
  • This ExpectedCondition isn't available in the C# version. – FDM Aug 3 '15 at 10:39
  • So, anything else i can do in C#? – MSeykov Aug 3 '15 at 10:49
  • Can you post the web page and your code? – FDM Aug 3 '15 at 12:03
  • I cant post the web page, because it contains information that i am not allowed to post here, but i can explain better my problem. In my positive test there is no problem with using button.click,but when i perform the negative test and i am submitting just after an error message for the current field is showed it says that the button is not clickable. If i use thread.sleep(1000) before i click the button, the problem is not present, but using the sleep method is not a good practice, so i want to use something that will check if the button is clickable and click the button only if that is true. – MSeykov Aug 3 '15 at 12:42
0

In many cases slenium does not get the element eventhough it is present. script works some times and fails as well. This code works when actaul element is present but selenium is not find it. I have create a loop for given count ant it returns true.

 public static bool waitTillElementisDisplayed(IWebDriver driver, By by, int timeoutInSeconds)
    {
        bool elementDisplayed = false;

        for (int i = 0; i < timeoutInSeconds; i++)
        {
            try
            {
                if (timeoutInSeconds > 0)
                {
                    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                    wait.Until(drv => drv.FindElement(by));
                }
                elementDisplayed = driver.FindElement(by).Displayed;
            }
            catch
            { }
        }
        return elementDisplayed;

    }
-1

//define method

public void WaitFindAndClick(IWebDriver parent, By by, int counter)

    {
        bool Displayed = false;

        for (int v = 0; v < counter; counter++)
        {
            try
            {
                Thread.Sleep(2000);
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                wait.Until((d) => { return parent.FindElement(by); });

                Displayed = parent.FindElement(by).Displayed;

                if (Displayed)
                {
                    parent.FindElement(by).Click();
                    break;
                }
            }
            catch
            {
            }
        }


    }

    // Call method

            testutil.WaitFindAndClick(driver, By.Id("btnApplyForFunding"), 10);
  • -1 for explicit sleep for 2 seconds. I've seen that become part of major systems without folks knowing. – Michael Durrant Jun 25 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.