Reputation: 299
I would like to use the getPageSource() method to save the current page source under a different name in a nominated folder. e.g. save current page source as Hawai.htm under C:/Holiday folder.
Most reference material including Java doc available have only touched on getPageSource() but nothing specifically not what is needed in this case.
I am using Selenium Webdriver 2 Java (JDK 7) on Windows platform.
Upvotes: 0
Views: 5276
Reputation: 5876
Based on documentation, the getPageSource()
might (depends on browser) not return correct content if page has been modified by JavaScript. If you have jQuery, you could use:
try (FileWriter fstream = new FileWriter("C://Holiday//Hawai.htm");
BufferedWriter out = new BufferedWriter(fstream)) {
String content = (String)((JavascriptExecutor)driver).executeScript("return $('html').html();"));
out.write(content);
}
catch (Exception e) {...}
Upvotes: 0
Reputation: 5667
getPageSource() will return a String which contains entire page source.
In WebDriver there is no file operations available. For writing that string (page source) to separate file in required location you should use some programming language.
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("C://Holiday//Hawai.htm");
BufferedWriter out = new BufferedWriter(fstream);
out.write(driver.getPageSource());
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Upvotes: 5