- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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 × 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/
昨天我更新了我的旧 chromedriver(v2.0.204324.dyu) 并在 https://code.google.com/p/chromedriver/downloads/list 上传了
我在使用 Selenium WebDriver.chromedriver 获取 json 结果数据时遇到问题,服务器将响应 http 状态代码 429 太多请求。 但是当我直接使用 chrome 获取
所以我在 stack here 上看到了一个类似的问题,但它没有被接受的答案,也没有为我提供我需要的信息。 我正在尝试使用“chromedriver”,因为“selenium-webdriver”需要
嗨,在 headless centos 7 中,我正在尝试进行 headless selenium 测试。为此,我安装了 google-chrome、chromedriver 和 selenium p
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我的问题与这篇文章有关:session not created: This version of ChromeDriver only supports Chrome version 74 error
我们试图在 Jenkins 上添加一些 selenium 脚本,但每当我们发出命令时,我都会收到错误消息“Chromedriver.exe 已停止工作”。 我将 chromedriver 移动到另一个
我使用的是 Macbook Pro,并且使用 Selenium 2.46.0 for Java 和 Chromedriver。我只有一个main方法,如下: public static void ma
我正在尝试在 heroku 上设置 selenium。我一直在看Running ChromeDriver with Python selenium on Heroku寻求帮助。基于此,我安装了列出的
当我设置初始设置 Nightwatchjs(使用初学者教程)时,出现如下错误: 尝试启动 ChromeDriver 时出错:无法解析路径:“/node_modules/.bin/chromedrive
我在 Windows 7 上使用 Selenium Chromedriver 时遇到问题。为了显示该问题,我将其简化为一个简单的脚本来简单地启动《纽约时报》网站: from selenium impo
问题 我一直在使用 Selenium 编写 Python 脚本,它在本地运行良好(因为我已将 ChromeDriver 安装在正确的目录中,即 assets/chromedriver),但它无法在赫罗
WebDriver driver = new ChromeDriver(options); 显示构造函数 ChromeDriver(ChromeOptions) 未定义,为什么? 最佳答案 导入以下语
我安装了 nuget 包 - Selenium.WebDriver.ChromeDriver 2.46.0.. 当我发布(通过 dotnet publish .Net CLI 命令).csprojec
我想用 chromedriver 从 fanfiction.net 上抓取一些故事。 我尝试以下方法: from selenium import webdriver import time path
当我尝试运行 npm install chromedriver 我得到... ChromeDriver installation failed Error extracting archive: Er
只是一个简单的 Python 代码: from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_
使用 C# 在 selenium 中哪里可以找到 chromedriver.log。在哪里可以看到chromedriver的日志文件? ChromeOptions optn= new ChromeOp
我在 Linux 上使用 Node 5.10.0。运行脚本时出现一些问题,如下所示 [davea@mydevbox mydir]$ node SkyNet.js Validation Complete
我是一名优秀的程序员,十分优秀!