Skip to main content
 首页 » 编程设计

c#之显式等待 Selenium Webdriver 获取手中的元素

2024年01月26日28小虾米

我在 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)); 

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

请您参考如下方法:

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

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