gpt4 book ai didi

c# - Selenium 和 ChromeDriver 偶尔会为 GET 请求返回错误的内容

转载 作者:太空狗 更新时间:2023-10-30 01:23:21 25 4
gpt4 key购买 nike

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

最佳答案

可能是因为您没有给驱动程序足够的时间来完成调用和加载页面,所以它会“返回”它返回的任何先前页面。您是否考虑过设置 timeout/wait在驱动程序上?

编辑

关于 为什么 在 Chrome 而不是其他浏览器中存在此问题的问题,我不得不冒险猜测并说这可能与不同浏览器的方式有关引擎处理直接显示图像而不是 HTML。我之所以做出这个假设,是因为在针对 HTML 页面(如 Google 主页)运行类似代码时,并未发现所描述的这种差异。

每个浏览器都将图像包装在一些 HTML 中。例如,IE9 这样包装:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY><IMG src="[url here]"></BODY></HTML>

而 Firefox 将其包装为:

<html>
<head>
<meta content="width=device-width; height=device-height;" name="viewport">
<link href="resource://gre/res/TopLevelImageDocument.css" rel="stylesheet">
<title>[filename] (JPEG Image, 500&nbsp;×&nbsp;332 pixels)</title>
</head>
<body>
<img alt="[url here]" src="[url here]">
</body>
</html>

最后,Chrome:

<html>
<body style="margin: 0px;">
<img style="-webkit-user-select: none; " src="[url here]" width="500" height="332">
</body>
<style type="text/css"></style>
</html>

现在不知道为什么Chrome版本会导致webdriver无法检测到pageload。它当然是三个 HTML 包装器中最小的一个,并且 w3 validator当被要求验证其 HTML 时,它有轻微的惊恐发作,而其他两个验证相对较好。

此外,正如 mootinator 所提到的,一般来说,有很多关于 Chrome 驱动程序的投诉,因此这可能只是 Chrome 网络驱动程序本身的问题。我刚刚发现上面的内容很有趣,并认为它可能值得分享。

关于c# - Selenium 和 ChromeDriver 偶尔会为 GET 请求返回错误的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com