使用Selenium保存页面 [英] Saving pages with Selenium
问题描述
我会再试一次.
下面的代码是我从另一个站点复制的,用户说它可以工作(显示屏幕截图).
The code below I copied from another site and the user say it works (shows a screenshot).Original code
我测试了代码:没有错误,但是没有文件保存.
I tested the code: No error, but no file save.
All questions use this answer to save a file: A question!
为什么不保存页面,或者如果不保存,文件在哪里?
why the page is not saved or, if it is, where is the file?
谢谢
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Selenium\chromedriver.exe")
driver.get("http://www.example.com")
saveas = ActionChains(driver).key_down(Keys.CONTROL).send_keys('S').key_up(Keys.CONTROL)
saveas.perform()
推荐答案
如果在浏览器中执行组合键,则只会看到保存页面"对话框.您还需要发送ALT + S来保存页面,在Windows中,默认情况下它将保存在下载"文件夹中.
If you do the key combination in the browser, you will see this only brings up the 'save page' dialog box. You need to additionally send ALT+S to save the page, in Windows it will be saved in your Downloads folder by default.
saveas = ActionChains(driver).key_down(Keys.CONTROL).send_keys('S').key_up(Keys.CONTROL).send_keys('MyDocumentName').key_down(Keys.ALT).send_keys('S').key_up(Keys.ALT)
ActionChains不可靠.不与浏览器GUI交互会更容易.
ActionChains are unreliable. It would be easier not to interact with the browser GUI.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Selenium\chromedriver.exe")
driver.get("http://www.example.com")
with open('page.html', 'w') as f:
f.write(driver.page_source)
这篇关于使用Selenium保存页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!