6

I am new to selenium webdriver and am testing one application. In my application, I have to test about linking Facebook Account. Whenever I click on link the pop up will be displayed where I have to enter credentials. I am able to link sometimes and sometimes the test fails. I know the reason because it takes time to load pop up window and next command is executed so it is not able to find the element. I have used Thread.Sleep but I want to use implicit wait or explicit wait which is always a good practice rather than Thread.Sleep. How to use implicit wait and where to use that command exactly? Please advice. Thanks.

 public void SocialFaceBook()
    {           
        string currentWindow = driver.CurrentWindowHandle;
        PopupWindowFinder finder = new PopupWindowFinder(driver);
        string facebookWindow = finder.Click(driver.FindElement(By.XPath("//div[@id='panelFacebook']/div[2]/div[3]/div[3]/a")));
        // Switch To FaceBook Window
        driver.SwitchTo().Window(facebookWindow);
        System.Threading.Thread.Sleep(3000);

        // Link
        // Email Address
        IWebElement faceBookLinkEmail = driver.FindElement(By.Id("email"));
        faceBookLinkEmail.SendKeys(SocialFaceBookEmail);

        // Password
        IWebElement faceBookLinkPass = driver.FindElement(By.Id("pass"));
        faceBookLinkPass.SendKeys(SocialFaceBookPass);

        // Log In Button
        IWebElement faceBookLinkLogin = driver.FindElement(By.XPath("//input[@id='u_0_1']"));
        faceBookLinkLogin.Click();

        // Switch To Main Window
        driver.SwitchTo().Window(currentWindow);
        System.Threading.Thread.Sleep(3000);

        // Sync            
        IWebElement faceBookSync = driver.FindElement(By.XPath("//div[@id='panelFacebook']/div[2]/div[3]/div[2]/a"));
        faceBookSync.Click();

        // Unlink
        IWebElement faceBookUnLink = driver.FindElement(By.XPath("//div[@id='panelFacebook']/div[2]/div[3]/div[1]/a"));
        faceBookUnLink.Click();
    }

Sometimes it is not able to find the log in details as pop up is not loaded properly and sometimes it is not able to find sync button as facebook account takes time to link. Please advice.

user3931772
  • 97
  • 1
  • 3
  • 10
  • See the following for additional information http://stackoverflow.com/questions/18063377/wait-for-an-element-using-selenium-webdriver either implicit or fluid wait should work – Mike Beeler Aug 19 '14 at 01:39
  • Various solutions were also provided here: http://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present. – Codatrix Aug 19 '14 at 01:42
  • @user3931772 I was able to get something that worked very well for me, while I was waiting for something to be visible before clicking on it. Thank you for asking this question! – tylerlindell Apr 08 '15 at 23:12

6 Answers6

19

WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("locator")));

It will wait for the element to be located for a maximum of 30 seconds if the element is found before that it will execute....

Vicky
  • 2,999
  • 2
  • 21
  • 36
  • thank you for your answer! It helped me find the solution that worked very well for me. I posted my solution here as well. – tylerlindell Apr 08 '15 at 23:10
  • Thanks, It helped me a lot! My second code row looks like `wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("#dummy-div")));` – userlond Dec 19 '15 at 01:17
  • use TimeSpan.FromSeconds(30) instead of 30 – Setmax Nov 26 '19 at 15:07
4

I had to change some small things from @Vicky's answer but this is what I got as a method I could call.

public static void WaitForElementLoad(By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutInSeconds));
            wait.Until(ExpectedConditions.ElementIsVisible(by));
        }
    }

and I call it like this:

MyClass.WaitForElementLoad(By.CssSelector("div#div1 div strong a"), 10);
tylerlindell
  • 1,545
  • 2
  • 17
  • 20
0

I used to write a function to detect an element exists every second. if find continue, otherwise threw timeout error. but in that function i still use Thread.Sleep.

Expect other good solution.

Dexter Yao
  • 44
  • 5
  • Thread.Sleep is not a good practice to work cause it will wait anyhow even if element is found or not. I tried using implicit wait but its making test fail. Don't know the reason but its not working. – user3931772 Aug 19 '14 at 03:26
0

I use ExpectedCondition.visibilityOf(element) instead of thread sleeps

bcar
  • 815
  • 9
  • 19
0

Implicit wait -- max waiting time to identify object, it will identify the object for every 500 ms. if it fails to identify the object with in maximum time it will throw nosuchelement exception.

Explicit wait -- used for ajax page loads for the same purpose.

maximum waiting time is same as thread.sleep

venkat
  • 111
  • 4
0

Wrote a method for a project:

public static void WaitForElementPresentBy(By by)
        {
            try
            {
                Wait.Until(ExpectedConditions.ElementIsVisible(by));
            }
            catch (TimeoutException te)
            {

                Assert.Fail("The element with selector {0} didn't appear. The exception was:\n {1}", by, te.ToString());
            }
        }

Wait should have already been defined.

Ukrainis
  • 534
  • 2
  • 16