显式地等待Selenium Webdriver获取手头的元素

本文关键字:元素 获取 Webdriver 等待 Selenium | 更新日期: 2023-09-27 18:12:36

我在c#上使用selenium webdriver,我使用页面对象模块。现在我需要一种语法来显式等待,因为我已经有了web元素。

[FindsBy(How = How.Id, Using = "Passwd")]
public IWebElement Password {get;set;}
[FindsBy(How = How.Id, Using = "signIn")]
public IWebElement Signin { get; set; }

我需要等待,直到我找到元素密码。

在使用这个模块之前,我正在使用:

WebDriverWait wait = new WebDriverWait(driver.driver, TimeSpan.FromSeconds(Time));
wait.Until(ExpectedConditions.ElementExists(by));

现在我需要使用手头的元素

显式地等待Selenium Webdriver获取手头的元素

您应该尝试使用ExpectedConditions.ElementToBeClickable,它将接受IWebElement以及输入,并将等待直到元素可见并启用,如下所示:-

WebDriverWait wait = new WebDriverWait(driver.driver, TimeSpan.FromSeconds(Time));
wait.Until(ExpectedConditions.ElementToBeClickable(Password));

请检查是否有帮助

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(driver => Password.Displayed);

添加显式等待,等待Password.Displayed为真:

[FindsBy(How = How.Id, Using = "Passwd")]
public IWebElement Password {get;set;}
[FindsBy(How = How.Id, Using = "signIn")]
public IWebElement Signin { get; set; }
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(Password.Displayed);

全面披露,我在这里找到了答案:https://groups.google.com/forum/#!主题/webdriver xdFsocNMSNc

预期条件方法以By为参数,您想使用ElementIsVisible,即以下应该工作:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Passwd")));