简体   繁体   English

使用Python Selenium保存网页

[英]Save a Web Page with Python Selenium

I am using selenium webdriver for Python 2.7: 我正在使用selenium webdriver for Python 2.7:

  1. Start a browser: browser = webdriver.Firefox() . 启动浏览器: browser = webdriver.Firefox()

  2. Go to some URL: browser.get('http://www.google.com') . 转到某个网址: browser.get('http://www.google.com')

At this point, how can I send a 'Save Page As' command to the browser? 此时,如何向浏览器发送“另存页面”命令?

Note: It is not the web-page source that I am interested in. I would like to save the page using the actual 'Save Page As' Firefox command, which yields different results than saving the web-page source. 注意:这不是我感兴趣的网页源。我想使用实际的'Save Page As'Firefox命令保存页面,这会产生与保存网页源不同的结果。

Unfortunately you can't do what you would like to do with Selenium. 不幸的是,你无法做你想做的Selenium。 You can use page_source to get the html but that is all that you would get. 您可以使用page_source来获取html,但这就是您将获得的所有内容。

Selenium unfortunately can't interact with the Dialog that is given to you when you do save as. 不幸的是,当您保存时,Selenium无法与提供给您的Dialog交互。

You can do the following to get the dialog up but then you will need something like AutoIT to finish it off 你可以执行以下操作来获得对话框,但是你需要像AutoIT这样的东西来完成它

from selenium.webdriver.common.action_chains import ActionChains

saveas = ActionChains(driver).key_down(Keys.CONTROL)\
         .send_keys('s').key_up(Keys.CONTROL)
saveas.perform()

I had a similar problem and solved it recently: 我有一个类似的问题并且最近解决了它:

@AutomatedTester gave a decent answer, but his answer did not solve the problem all the way, you still need to press Enter one more time yourself to finish the job. @AutomatedTester给出了一个不错的答案,但他的答案并没有解决问题,你还需要再次按Enter键才能完成这项工作。

Therefore, we need Python to do press that one more Enter for us. 因此,我们需要Python为我们再按一次Enter。

Follow @NoctisSkytower 's answer in the following thread: 在以下主题中关注@NoctisSkytower的回答:

Python simulate keydown Python模拟keydown

copy his definition for classes and then add the following to @AutomatedTester 's answer: 复制他对类的定义,然后将以下内容添加到@AutomatedTester的答案中:

SendInput(Keyboard(VK_RETURN))
time.sleep(0.2)
SendInput(Keyboard(VK_RETURN, KEYEVENTF_KEYUP))

you may also want to check out the following link: 您可能还想查看以下链接:

How can selenium web driver get to know when the new window has opened and then resume its execution selenium web驱动程序如何知道新窗口何时打开然后恢复执行

You may encounter pop-up window and this thread will tell you want to do. 您可能会遇到弹出窗口,此线程会告诉您要执行此操作。

If you are using Linux, you can use xte for that. 如果您使用的是Linux,则可以使用xte Install 安装

sudo apt-get install xautomation

first. 第一。

Example

from subprocess import Popen, PIPE

save_sequence = """keydown Control_L
key S
keyup Control_L
"""


def keypress(sequence):
    p = Popen(['xte'], stdin=PIPE)
    p.communicate(input=sequence)

keypress(save_sequence)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM