4

In Selenium 2, the WebDriver object only offers a method getPageSource() which saves the raw HTML page without any CSS, JS, images etc.

Is there a way to also save all referenced resources in the HTML page (similar to HtmlUnit's HtmlPage.save())?

3 Answers 3

1

I know I'm royally late with my answer, but I didn't really find an answer for this question when I was searching myself. So I did something myself, hope I can help some people still.

For c# here's how I did it:

using system.net;

string DataDirectory = "C:\\Temp\\AutoTest\\Data\\";
string PageSourceHTML = Driver.PageSource;

string[] StringSeparators = new string[] { "<" };
string[] Result = PageSourceHTML.Split(StringSeparators, StringSplitOptions.None);
string CSSFile;

string FileName = "filename.html";
System.IO.File.WriteAllText(DataDirectory + FileName, PageSourceHTML);

foreach(string S in Result)
{
    if(S.Contains("stylesheet"))
    {
        CSSFile = S.Substring(28); // strip off "link rel="stylesheet" href="
        CSSFile = CSSFile.Substring(0,CSSFile.Length-10); // strip off characters behind, like " />" and newline, spaces until next "<" was found. Can and probably will be different in your case.
        System.IO.Directory.CreateDirectory(DataDirectory + "\\" + CSSFile.Substring(0, CSSFile.LastIndexOf("/"))); //create the CSS direcotry structure
        var Client = new WebClient();
        Client.DownloadFile(Browser.Browser.WebUrl + "/" + CSSFile, DataDirectory + "\\" + CSSFile); // download the file and save it with the same filename under the same relative path.
    }
}

I'm sure it could be improved to include any unforeseen situations, but for my website in test it will always work like this.

0

Nope. If you can, go for HtmlUnit for this particular task.

The best you could do, I think, is Robot. Press Ctrl + S simultaneously, the confirm with Enter. It's blind, it's imperfect, but it's the closest thing to your need.

0

You can use the selenium interactions to handle it.

    using OpenQA.Selenium.Interactions;

There are a few ways to do it as well. One of the ways that I handle something like this, is to find an item central to the page, or whichever area that you wish to save, and do an actions builder.

    var htmlElement = driver.FindElement(By.XPath("//your path"));
    Actions action = new Actions(driver);
    try
    {
     action.MoveToElement(htmlElement).ContextClick(htmlElement).SendKeys("p").Build().Perform();
    }
    catch(WebDriverException){}

This will simply right click on the area, and then send the key "p" which is the 'Save Page As' hotkey in firefox when right clicking. Another way is to have the builder send the keys.

    var htmlElement = driver.FindElement(By.Xpath("//your path"));
    action.MoveToElement(htmlElement);
    try 
    {
      action.KeyDown(Keys.Control).SendKeys("S").KeyUp(Keys.Control).Build().Perform();
    }
    catch(WebDriverException){}

Note that in both cases, if you leave the scope of the driver, say a windows form, then you will have to switch your case / code to handle the windows form when it pops up. Selenium will also have issues with nothing being returned after the keys are sent, so the Try Catches are there for that. If anyone has a way to work around that, it would be awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.