C# Download PDF File Embeded

145 views
Skip to first unread message

Paulo Nunes

unread,
Jan 17, 2022, 12:35:46 AM1/17/22
to Selenium Users
I have a c# program which is searching for files in a web site and I've managed to reach to a page which has the pdf embedded
img1.png

The link for that page is something like the following
https://<somesite>/GetPdf.aspx?w=OgKRY780WZSjZ3jDC1L2Ir8qK0l96Wl1SyFrU3OHY0yt809FEB588LqCB7F%2f8PsWmYrZM0TR%2fb8Y9x3Suu8ju%2bZ%2f3kZTsJ%2bu5eONMKcu8n8sjxe4IkKD5jjc1N0uBDm%2fAjowXGSJKW%2fQhauxrfD2TawYVDAn2iIydlByTcmk4iJLd1xFJgsWlnwmW9J7nHi2DhvrEVBK82XGwwCKdsT7%2bg%3d%3d

And Selenium shows this as page source:

<html><head></head><body style="height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);"><embed name="F785A17241B40FCC2739D64E4350F6F4" style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="about:blank" type="application/pdf" internalid="F785A17241B40FCC2739D64E4350F6F4"></body></html>

My code is as following

var options = new ChromeOptions();
options.AddArguments("--incognito");
 options.AddArgument("--start-maximized");
 ChromeDriver cd = new ChromeDriver(@"C:\Temp\chrome", options);
cd.Url = @"Somesite/login.aspx"; cd.Navigate();
 ...
 e.Click();
 IWebElement ifamepdf = divform.FindElement(By.TagName("iframe"));
string src = ifamepdf.GetAttribute("src");
WebClient wc = new WebClient();
wc.DownloadFile(new Uri(src), @"c:\temp\teste6.pdf");
wc.Dispose();
 IWebElement ifameembed = cd.FindElement(By.TagName("embed"));
string source2 = cd.PageSource;
Console.WriteLine(source2);

How can I download the PDF?
Reply all
Reply to author
Forward
0 new messages