barak manos
barak manos

Reputation: 30136

Save a Web Page with Python Selenium

I am using selenium webdriver for Python 2.7:

  1. Start a browser: browser = webdriver.Firefox().

  2. Go to some URL: 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.

Upvotes: 12

Views: 14266

Answers (3)

Martin Thoma
Martin Thoma

Reputation: 136247

If you are using Linux, you can use xte for that. 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)

Upvotes: 0

AutomatedTester
AutomatedTester

Reputation: 22418

Unfortunately you can't do what you would like to do with Selenium. You can use page_source to get the html but that is all that you would get.

Selenium unfortunately can't interact with the Dialog that is given to you when you do save as.

You can do the following to get the dialog up but then you will need something like AutoIT to finish it off

from selenium.webdriver.common.action_chains import ActionChains

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

Upvotes: 11

Niebieski
Niebieski

Reputation: 609

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.

Therefore, we need Python to do press that one more Enter for us.

Follow @NoctisSkytower 's answer in the following thread:

Python simulate keydown

copy his definition for classes and then add the following to @AutomatedTester 's answer:

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

You may encounter pop-up window and this thread will tell you want to do.

Upvotes: 5

Related Questions