C# 在不使用Thread.sleep的情况下单击网页后,等待弹出警报
单击网页上的搜索后,将出现警报弹出窗口。我需要等到弹出窗口出现。我不得不不用线程。睡眠 您可以使用C# 在不使用Thread.sleep的情况下单击网页后,等待弹出警报,c#,selenium,C#,Selenium,单击网页上的搜索后,将出现警报弹出窗口。我需要等到弹出窗口出现。我不得不不用线程。睡眠 您可以使用wait命令: WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>)); WebDriverWait wait=new-WebDriverWait(webDr
wait
命令:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
WebDriverWait wait=new-WebDriverWait(webDriver,timeoutingseconds);
等待.直到(预期条件.元素的可视性(通过.id));
ExpectedConditions类具有特定的等待警报弹出窗口
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.alertIsPresent());
这有点粗鲁,但为我工作
public IAlert WaitAlert(int timeOut)
{
for (int cont = timeOut; cont > 0; cont--)
{
try
{
return driver.SwitchTo().Alert();
}
catch (NoAlertPresentException erro)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
}
}
throw new NoAlertPresentException();
}
ExpectedConditions
已过时,请尝试使用此选项:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());
试试这个,缺点是这个没有超时。必须确保弹出窗口将出现
while (driver.WindowHandles.Count == 1)
{
//leave it blank
}
这个怎么样
public static class SeleniumExtensions
{
public static IAlert WaitAlert(this ITargetLocator locator, int seconds = 10)
{
while (true)
{
try { return locator.Alert(); }
catch (NoAlertPresentException) when (--seconds >= 0) { Thread.Sleep(1000); }
}
}
}
为我工作:
var alert = _driver.SwitchTo().WaitAlert();