如何等待元素硒的webdriver加载? [英] How to wait for element to load in selenium webdriver?

查看:143
本文介绍了如何等待元素硒的webdriver加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来硒的webdriver和正在测试一个应用程序。在我的申请,我一定要考有关链接Facebook帐户。每当我点击链接弹出将被显示在那里我输入凭据。我有时能,有时测试失败联系起来。我知道原因,因为它需要时间来加载弹出窗口,并执行下一个命令,以便它不能找到的元素。我已经使用了Thread.Sleep但我想使用隐等待或等待明确这始终是一个很好的做法,而不是Thread.sleep代码。如何使用隐式的等待并在准确使用这个命令?请指教。谢谢



 公共无效SocialFaceBook()
{
串currentWindow = driver.CurrentWindowHandle;
PopupWindowFinder取景器=新PopupWindowFinder(驱动器);
串facebookWindow = finder.Click(driver.FindElement(By.XPath(// DIV [@ ID ='panelFacebook'] / DIV [2] / DIV [3] / DIV [3] / A) ));
//切换到FaceBook的窗口
driver.SwitchTo()窗口(facebookWindow)。
System.Threading.Thread.Sleep(3000);

//链接
//邮箱地址
IWebElement faceBookLinkEmail = driver.FindElement(By.Id(电子邮件));
faceBookLinkEmail.SendKeys(SocialFaceBookEmail);

//密码
IWebElement faceBookLinkPass = driver.FindElement(By.Id(通行证));
faceBookLinkPass.SendKeys(SocialFaceBookPass);

//登录按钮
IWebElement faceBookLinkLogin = driver.FindElement(By.XPath(//输入[@ ID ='u_0_1']));
faceBookLinkLogin.Click();

//切换到主窗口
driver.SwitchTo()窗口(currentWindow)。
System.Threading.Thread.Sleep(3000);

//同步
IWebElement faceBookSync = driver.FindElement(By.XPath(// DIV [@ ID ='panelFacebook'] / DIV [2] / DIV [3] / DIV [2] /一个));
faceBookSync.Click();

//取消链接
IWebElement faceBookUnLink = driver.FindElement(By.XPath(// DIV [@ ID ='panelFacebook'] / DIV [2] / DIV [3] / DIV [1] /一个));
faceBookUnLink.Click();
}



有时是无法找到在日志中细节弹出是不是正确加载,有时它是不是能够找到同步按钮为Facebook账户需要时间来联系起来。请指教


解决方案

WebDriverWait等待=新WebDriverWait(驱动程序,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(定位器)));




这将等待该元素将设最多30秒元素是否被发现之前,它会执行....


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.

解决方案

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

这篇关于如何等待元素硒的webdriver加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆