How to save page in Firefox, Chrome, PhantomJS wit

2019-03-30 21:34发布

问题:

I want to save a web site and download file into specific folder. How can I do it with webdriver? In Chrome and Firefox I just press Ctrl+S then select a folder to save the current website. Is there any way to do it in selenium webdriver? Can PhantomJS do it?

回答1:

My conclusion is that you cannot do this with the current features of selenium.

Why? The two available features from Selenium that are relevant for your needs are:

  • taking screenshot
  • and retrieving the html source of the page

However those won't match what is done with a Ctrl+S.



回答2:

Automate pressing Control S then switch to the save menu and save the html file. Here's how I did it with Python:

    driver.send_keys("u'\ue009'"+"s")
    driver.switch_to.window("Window_ID")
    driver.find_element_by_id("SAVE_button").click()

Use the inspect tool to find the Window name and the necessary Id's.

see: http://selenium-python.readthedocs.org/api.html#selenium.webdriver.common.keys.Keys.CONTROL