Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 631
barak manos Avatar asked Jun 10 '12 08:06

barak manos


2 Answers

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()
like image 80
AutomatedTester Avatar answered Nov 15 '22 14:11

AutomatedTester


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.

like image 27
Niebieski Avatar answered Nov 15 '22 16:11

Niebieski