Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在不使用Thread.sleep的情况下单击网页后,等待弹出警报_C#_Selenium - Fatal编程技术网

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();