I download PDF file using Selenium, Chrome driver, NUnit and C# and a saved file has no images - only text

1,001 views
Skip to first unread message

Lukasz Gruszkiewicz

unread,
Apr 26, 2020, 6:50:07 AM4/26/20
to Selenium Users

I try to download and analyze the content of PDF file from tested Angular Application. I have tried two methods: 1) First method allows Selenium to download PDF file to specified location thanks to several Chrome driver options:

        ChromeOptions options = new ChromeOptions();
        options.AddUserProfilePreference("download.default_directory", path);
        options.AddUserProfilePreference("download.prompt_for_download", false); 
        options.AddUserProfilePreference("download.directory_upgrade", "true");
        options.AddUserProfilePreference("disable-popup-blocking", "true");
        options.AddUserProfilePreference("safebrowsing.enabled", "true");
        options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
        options.AddArguments("--no-sandbox");
        options.AddArguments("--disable-gpu");

Then I use location from download.default_dictionary to get and analyze file using third party library:

        string folderPath = Path.Combine(downloadFolder, _context.GetTestRunId().ToString(), _context.GetTestName());
        string file = Path.Combine(folderPath, fileName); [...]
        Ldoc = new PdfLoadedDocument(file);

2) Second method is to allowed Chrome driver to open PDF file in another tab in browser:

        options.AddUserProfilePreference("plugins.always_open_pdf_externally", false);

and switch to this new tab:

            var currentscreen = GetDriver().CurrentWindowHandle;
            var tabSet = GetDriver().WindowHandles;
            foreach (string tab in tabSet)
            {
                if (!tab.Equals(currentscreen))
                {
                    GetDriver().SwitchTo().Window(tab);
                }
            }

and then get file from url and save it to given location and then analyze it:

        string folderPath = Path.Combine(downloadFolder, _context.GetTestRunId().ToString(), _context.GetTestName());
        string file = Path.Combine(folderPath, fileName);

        var url = _context.GetDriver().Url;
        byte[] imageData = null;

        using (var wc = new System.Net.WebClient())
            imageData = wc.DownloadData(url);

        var ms = new MemoryStream(imageData);
        FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write);
        ms.WriteTo(fileStream);
        fileStream.Close();
        ms.Close();

In both cases when I execute this test from my local machine form Visual Studio and url of application is on test server - downloaded pdf file consists of text and one image. But when TFS runs the same test - PDF file dowloaded to the location on a server is missing this image.I have also tried this approach: Missing elements when using selenium chrome driver to automatically 'Save as PDF' but it doesn't work in my case.


I have asked this question here too but with no success: https://stackoverflow.com/questions/61418204/i-download-pdf-file-using-selenium-chrome-driver-nunit-and-c-sharp-and-a-saved

STACKOVERFLAW.png
Reply all
Reply to author
Forward
0 new messages