1

调查 C# 的 PhantomJSDriver。我发现了一个问题:当我尝试单击调用文件保存对话框的元素时 - PhantomJSDriver 抛出异常:

错误消息 => '单击成功但加载失败。状态:'失败''

由 Request => {"headers":{"Accept":"application/json, image/png","Connection":"Close","Content-Length":"0","Content-Type":" 引起application/json;charset=utf-8","Host":"localhost:34660"},"httpVersion":"1.1","method":"POST","post":"","url":" /click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","re​​lative ":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/8839b140-08d7-11e3 -9482-fda3a2d41a6f/element/%3Awdc%3A1376920756991/click"}

PhantomJSDriver 是否有任何选项可以处理文件保存?

C#代码:

var dr = new PhantomJSDriver();

dr.Navigate().GoToUrl(" http://www.mywebapps.com/Default.aspx ");

var link = dr.FindElementByXPath("//a[@href='Setup.exe']");

链接.点击();

4

1 回答 1

0

PhantomJS 报告单击失败,因为它正在检查页面内容是否有效。在这种情况下,由于您单击要下载的 .exe 链接,它会报告单击失败。

如果要下载文件,而不是单击链接,使用 aWebClient直接从 URL 下载它。

using (WebClient webClient= new WebClient())
{
    webClient.DownloadFile("http://yoururl/Setup.exe", "c:/temp/setup.exe");
}

您应该阅读这篇博客文章如何使用 Selenium 下载文件以及为什么不应该,如果它没有说服您放弃它,它确实提供了一些关于如何完成文件下载的非常好的代码示例。

于 2013-08-19T20:31:39.930 回答