gpt4 book ai didi

c# - 使用 PhantomJS 下载文件

转载 作者:行者123 更新时间:2023-12-04 09:14:57 26 4
gpt4 key购买 nike

我正在尝试使用 PhantomJS 下载文件,但是当我点击下载时,没有下载任何文件,我读到 Phantomjs 不支持下载,但我需要那个,你能帮我吗?

这是我尝试下载时的部分代码:

try
{
checkbox = wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("//form[@id='formDownload']//table[@class='fileTables']//tbody//tr//td//input[@class='checkbox']")));
checkbox[countLine - 1].Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("all"))).Click();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

最佳答案

好的,你需要做的是:

  1. 当您单击 html 中的文件时,您需要找到该文件的链接。

  2. 您需要获取链接并发出 httpRequest 来获取文件。

这是请求的完整功能(我为您提供了方便,只需找到链接即可)

public static bool DownloadFile(string url, IWebDriver driver)
{
try
{
// Construct HTTP request to get the file
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.CookieContainer = new System.Net.CookieContainer();

for (int i = 0; i < driver.Manage().Cookies.AllCookies.Count - 1; i++)
{
System.Net.Cookie ck = new System.Net.Cookie(driver.Manage().Cookies.AllCookies[i].Name, driver.Manage().Cookies.AllCookies[i].Value, driver.Manage().Cookies.AllCookies[i].Path, driver.Manage().Cookies.AllCookies[i].Domain);
httpRequest.CookieContainer.Add(ck);
}

httpRequest.Accept = "text/html, application/xhtml+xml, */*";
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";

//HttpStatusCode responseStatus;

// Get back the HTTP response for web server
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();

// Define buffer and buffer size
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;

// Read from response and write to file
FileStream fileStream = File.Create(FilePath + "\\" + FileName + ".xls");
while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0)
{
fileStream.Write(buffer, 0, bytesRead);
}

return true;
}
catch (Exception ex)
{
return false;
}
}

关于c# - 使用 PhantomJS 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513538/

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