Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c# - How to wait for element to load in selenium webdriver?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...