【问题标题】:Selenium, exception on getting a file in IE using WebClientSelenium,使用 WebClient 在 IE 中获取文件时出现异常
【发布时间】:2014-07-03 09:49:35
【问题描述】:

我有一个 Selenium 测试,它应该下载一个文件并检查它是否包含某些内容。测试在 Firefox 和 Chrome 中运行正常,但在 IE 11 中出现异常。

IE 驱动程序:IEDriverServer 版本 2.41.0.0

代码:

<code>
 var driver = new InternetExplorerDriver();
 //....navigate and login to a page
 var el = driver.FindElements(By.PartialLinkText("Download File"))
                                             .First(p => p.GetAttribute("href").Contains("/downloadmyfile"));
 var link = el.GetAttribute("href");
 var client = new WebClient();
 client.Headers[HttpRequestHeader.Cookie] = CookieString();
 var data = client.DownloadString(new Uri(link));
</code>

复制cookies的代码:

<code>
  public string CookieString()
    {
        var cookies = driver.Manage().Cookies.AllCookies;
        return string.Join("; ", cookies.Select(c => string.Format("{0}={1}", c.Name, c.Value)));
    }
</code>

例外:

  • 消息:底层连接已关闭:接收时发生意外错误。
  • InnerException:无法从传输连接读取数据:现有连接被远程主机强行关闭。

Firefox 驱动程序的 driver.Manage().Cookies.AllCookies 看起来:

ASP.NET_SessionId=sesionId222222; .authcookie=xxxxxxxx; __utma=1.559549671.1404381398.1404381398.1404381398.1; __utmb=1.1.10.1404381398; __utmc=1; __utmz=1.1404381398.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); testingSite=culture=en

IE 驱动的 driver.Manage().Cookies.AllCookies 看起来:

__utma=1.342629986.1404310901.1404378456.1404381205.3; __utmb=1.2.10.1404381205; __utmc=1; __utmz=1.1404310901.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); testingSite=culture=en

对于同一个网页,IE浏览器的浏览器cookie有sessionId和.authcookie。

谁知道 IE 驱动程序会出现什么问题?线索

我看到 IE 驱动程序不包含 _SessionId 和 .authcookie 的 cookie 值 有没有办法获取 IE 驱动程序丢失的 cookie?

  • 内部异常堆栈跟踪:

    在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.FixedSizeReader.ReadPacket(字节 [] 缓冲区,Int32 偏移量,Int32 计数) 在 System.Net.Security._SslStream.StartFrameHeader(Byte[] 缓冲区,Int32 偏移量,Int32
    计数,AsyncProtocolRequest asyncRequest) 在 System.Net.Security._SslStream.StartReading(Byte[] 缓冲区,Int32 偏移量,Int32
    计数,AsyncProtocolRequest asyncRequest) 在 System.Net.Security._SslStream.ProcessRead(Byte[] 缓冲区,Int32 偏移量,Int32 计数, AsyncProtocolRequest asyncRequest) 在 System.Net.TlsStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.PooledStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.Connection.SyncRead(HttpWebRequest 请求,布尔用户检索流, Boolean probeRead)

    • 异常堆栈跟踪:
-at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
 at System.Net.WebClient.DownloadString(Uri address)
 at MyClasss........

【问题讨论】:

  • 我发现问题出在IE驱动,无法获取cookies。
  • 请跟踪堆栈。什么版本的IE和什么版本的IEDriver?手动访问网站时 IE 中的 cookie 看起来是否相同?
  • 我已经安装了最新版本的IE 11。selenium的IE驱动是IEDriverServer.exe版本2.41.0.0。 fiddler中的cookie有sessionId和authCookie。

标签: internet-explorer selenium setcookie webclient


【解决方案1】:

这是因为 IE 不返回 HttpOnly cookie。 ASP.NET SessionID 是一个 HttpOnly cookie。

我们没有找到解决方案,因此我们转而使用我们注入页面的 JavaScript-AJAX 请求执行下载。我们还将结果作为编码字符串返回给 Selenium,因此我们不会在 HDD 中包含下载文件的垃圾邮件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多