简体   繁体   English

如何在 Selenium Webdriver Javascript 中另存为网页?

[英]How to save as web page in Selenium Webdriver Javascript?

I am trying to save a complete webpage using Ctrl+S in Selenium Webdriver JS (Firefox).我正在尝试在 Selenium Webdriver JS (Firefox) 中使用 Ctrl+S 保存完整的网页。 I can call out the save as dialog using this code:我可以使用以下代码调出另存为对话框:

driver.findElement(webdriver.By.tagName('html')).sendKeys(Key.CONTROL + 's');

Now the dialog is out.现在对话框出来了。 But I dont know how to name the file nor how to hit the enter.但我不知道如何命名文件,也不知道如何按下回车键。 Is there anyway to do this (interact with the dialog) in Selenium Webdriver JS or maybe even bypass the save as dialog and just save it onto the disk?无论如何在Selenium Webdriver JS中执行此操作(与对话框交互),或者甚至绕过另存为对话框并将其保存到磁盘上?

Thank you very much.非常感谢。

Selenium does not interact with native dialogs. Selenium 不与本机对话框交互。 You can try a workaround for this.您可以尝试解决此问题。

Instead of saving the web page, you can save the view-source in a file.您可以将view-source保存在文件中,而不是保存网页。

Let's say you want to save google.com page.假设您想保存google.com页面。

  • Open url with view-source like so.像这样打开带有view-source url。

driver.get("view-source:http://www.google.com");

  • Select all the contents of page by simulating Ctrl+a and Ctrl+c like so.像这样模拟Ctrl+aCtrl+c选择页面的所有内容。 This is dotnet code example.这是 dotnet 代码示例。 You can find something similar in Js.你可以在 Js 中找到类似的东西。
Actions action = new Actions(driver);
action.KeyDown(Keys.LeftControl)
      .SendKeys("a")
      .SendKeys("c")
      .Build()
      .Perform();
  • Format data store we stored in clipboard.我们存储在剪贴板中的格式数据存储。

string source = Clipboard.GetText(TextDataFormat.UnicodeText);

  • Save to file.保存到文件。

File.WriteAllText(@"PathToSaveTheSource", source);

This is a demo in Java language:这是 Java 语言的演示:

/* 1. get current web page source which is the complete html */
String pageHtmlSource = driver.getPageSource();
// you will get things like this:
// <html>
//   <head>
//   ...
//   </head>
//   <body>
//   ...
//   </body>
// </html>


/* 2. then save the String into a file */

Note that there are some differences between driver.getPageSource() and Ctrl+s .请注意, driver.getPageSource()Ctrl+s之间存在一些差异。 driver.getPageSource() only gets the html source, while Ctrl+s can also saves js/css. driver.getPageSource()只获取html源码, Ctrl+s也可以保存js/css。

Alternate Solution:替代解决方案:

The "Save As" dialog cannot be interacted with via Selenium (you need some external program). “另存为”对话框无法通过 Selenium 进行交互(您需要一些外部程序)。 I use require('child_process').exec() to run a .bat file to run a .vbs script that sends keys.我使用require('child_process').exec()运行 .bat 文件来运行发送密钥的 .vbs 脚本。

You can bypass the "Save As" dialog by giving a default download directory to chromedriver: #setDownloadPath .您可以通过为 chromedriver 提供默认下载目录来绕过“另存为”对话框: #setDownloadPath

I have not seen a firefox alternative for this.我还没有看到 Firefox 替代品。

Sadly, this code to simulate "Ctrl" + S is not working for me:可悲的是,这个模拟“Ctrl”+ S的代码对我不起作用:

driver.findElement(webdriver.By.tagName('html')).sendKeys(Key.CONTROL + 's');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver在网页上获取Ajax添加的内容? - How to get ajax added contents on web page using selenium webdriver? Selenium-webdriver / Javascript:如何滚动页面? - Selenium-webdriver/Javascript: How do I scroll through a page? 从Selenium WebDriver执行页面的JavaScript - Executing the page's JavaScript from Selenium WebDriver 如何使用javascript使用java使用selenium Webdriver设置所选Web元素的属性? - How to use javascript to set attribute of selected web element using selenium Webdriver using java? How to find web element by xpath using javascript code in python selenium webdriver? - How to find web element by xpath using javascript code in python selenium webdriver? 如何使用Javascript导出到Selenium IDE中的Webdriver代码 - How to export to webdriver code in Selenium IDE with Javascript Selenium webdriver:如何在控制台中显示值(Javascript) - Selenium webdriver : How to display values in console (Javascript) 如何使用Selenium Webdriver处理Javascript单击? - How to handle Javascript click by using selenium Webdriver? 如何使用webdriver(JavaScript)在selenium中停止测试? - How to stop tests in selenium using webdriver (JavaScript)? 如何在 Selenium WebDriver Java 中使用 JavaScript - How to use JavaScript with Selenium WebDriver Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM