C# 中的控制台应用程序在紧密循环中请求四个图像有时会返回先前的请求。代码如下,适用于任何网站,每次运行我通常会看到 3 或 4 个错误。我根据人们浏览我管理的网站的报告开发了此代码,当用户请求 HTML 页面时,偶尔会加载 jpeg 或脚本。
我不知道这是 Chrome 还是 ChromeDriver 的问题。如果之前的请求是一个 HTML 页面,那么您最终可以得到它而不是图像。似乎是一个比赛条件。
有没有其他人看到过这种行为,他们可以用下面的代码重复它吗?
class ContentVerify
{
OpenQA.Selenium.IWebDriver driver;
readonly System.Collections.Generic.List<string> testUrls = new System.Collections.Generic.List<string>()
{
"http://i.imgur.com/zNJvS.jpg",
"http://i.imgur.com/lzVec.jpg",
"http://i.imgur.com/rDuhT.jpg",
"http://i.imgur.com/sZ26q.jpg"
};
public void Check()
{
driver = new OpenQA.Selenium.Chrome.ChromeDriver(); // Both InternetExplorerDriver and FirefoxDriver work OK.
for (int i = 0; i < 10; i++)
{
TestUrls();
}
driver.Quit(); // The driver also crashes on exit, but this seems to be a known bug in Selenium.
}
private void TestUrls()
{
foreach (var item in testUrls)
{
System.Console.WriteLine(item);
//System.Threading.Thread.Sleep(1); // Uncommenting this makes Chrome & ChromeDriver work as expected.
driver.Url = item;
// Requests for images come back as an HTML image tag wrapped in a brief HTML page, like below;
//<html><body style="margin: 0px;"><img style="-webkit-user-select: none" src="http://i.imgur.com/zNJvS.jpg"></body></html>
// So the image should always be in the page, but sometimes (not always) we get the previous image requested.
if (!driver.PageSource.Contains(item))
{
System.Console.ForegroundColor = System.ConsoleColor.Red;
System.Console.WriteLine("Expected: {0}, got: {1}", item, driver.PageSource);
System.Console.ResetColor();
}
}
}
}