关于如何 运行 Selenium 测试以检查网站是否存在的任何建议?

Any suggestions on how to run a Selenium test so it checks whether a website exists?

我需要运行 进行测试以确认可以从网络上获取网站。这是我到目前为止所拥有的,但我很确定这是错误的,任何输入或提示将不胜感激,谢谢!

        var chromeOptions = new ChromeOptions();
        chromeOptions.AddArguments("headless");

        using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), chromeOptions))
        {
            driver.Navigate().GoToUrl("https://example.azurewebsites.net/");
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            wait.Until(d => d.Title.StartsWith("", StringComparison.OrdinalIgnoreCase));
            Assert.AreEqual(driver.Title, "https://example.azurewebsites.net/");
        }

如果您的目的只是检查站点是否存在,HttpRequest 是比 Selenium 更好的方法。您可以向 URL 发送一个简单的 GET 请求并检查状态代码。 如果该网站还活着,您将获得 200。 我以前用这种方法检查路由器连接状态。 我向“https://www.google.com/”发送了 GET 请求并检查了状态。 希望这有帮助。

要检查网站是否正常运行,您可以使用以下代码:

Java:

public boolean checkWebStatus(String url, int timeout) {
        HttpURLConnection connection = (HttpURLConnection) new URL(url)
                .openConnection();
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);
        connection.setRequestMethod("HEAD");
        int responseCode = connection.getResponseCode();
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
            return true;
        }
        else
          return false;

}

C#

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)

或者您可以针对 URL

执行 HTTP HEAD 请求
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("your url");
request.AllowAutoRedirect = false;
request.Method = "HEAD";
try {
    response = request.GetResponse();
    //Check response here
} catch (WebException wexp)
{
    //Catch exception
}

注意: 使用 SELENIUM 来检查站点是否正常运行是不可取的,而且比上面的实现慢得多.