我有一个硒脚本,带有If子句,因此只要该元素存在,就单击它,否则返回消息。
If (driver.FindElement(By.Id("id")).Enabled)
{
driver.FindElement(By.Id("id")).Click();
}
else
{
Console.WriteLine("Logout button was not visible!");
}
元素不存在,并且在到达If语句之前测试失败。我都尝试过.Displayed
/.Enabled
可能在尝试单击注销元素之前未加载它,您可以WebDriverWait
用来等待该元素可单击。将以下行添加到.cs文件的顶部
using OpenQA.Selenium.Support.UI;
然后,尝试以下操作:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("id"))).Click();
}
catch (WebDriverTimeoutException)
{
Console.WriteLine("Logout button was not visible!");
}
在WebDriverWait wait
将等待长达10秒抛出超时之前,如果它抛出,你可以写在控制台超时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句