如何以不同的名称和名称保存当前页面源文件夹 [英] How to save current page source in different name & folder

查看:146
本文介绍了如何以不同的名称和名称保存当前页面源文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用getPageSource()方法将当前页面源以不同的名称保存在指定的文件夹中.例如保存当前页面源 C:/Holiday文件夹下的Hawai.htm.

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.

包括Java doc在内的大多数参考资料仅涉及getPageSource(),但在这种情况下并不需要特别的内容.

Most reference material including Java doc available have only touched on getPageSource() but nothing specifically not what is needed in this case.

我正在Windows平台上使用Selenium Webdriver 2 Java(JDK 7).

I am using Selenium Webdriver 2 Java (JDK 7) on Windows platform.

推荐答案

getPageSource()将返回一个包含整个页面源代码的字符串.

getPageSource() will return a String which contains entire page source.

在WebDriver中,没有文件操作可用.要将该字符串(页面源)写到所需位置的单独文件中,您应该使用某种编程语言.

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());
  }
  }
}

这篇关于如何以不同的名称和名称保存当前页面源文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆