title: Save complete web page (incl css, images) using python/selenium
body: <p>I am using Python/Selenium to submit genetic sequences to an online database, and want to save the full page of results I get back. Below is the code that gets me to the results I want:</p>
  
  <pre><code>from selenium import webdriver
  
  URL = 'https://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastx&amp;PAGE_TYPE=BlastSearch&amp;LINK_LOC=blasthome'
  SEQUENCE = 'CCTAAACTATAGAAGGACAGCTCAAACACAAAGTTACCTAAACTATAGAAGGACAGCTCAAACACAAAGTTACCTAAACTATAGAAGGACAGCTCAAACACAAAGTTACCTAAACTATAGAAGGACAGCTCAAACACAAAGTTACCTAAACTATAGAAGGACA' #'GAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGAGAAGA'
  CHROME_WEBDRIVER_LOCATION = '/home/max/Downloads/chromedriver' # update this for your machine
  
  # open page with selenium
  # (first need to download Chrome webdriver, or a firefox webdriver, etc)
  driver = webdriver.Chrome(executable_path=CHROME_WEBDRIVER_LOCATION)
  driver.get(URL)
  time.sleep(5)
  
  # enter sequence into the query field and hit 'blast' button to search
  seq_query_field = driver.find_element_by_id("seq")
  seq_query_field.send_keys(SEQUENCE)
  
  blast_button = driver.find_element_by_id("b1")
  blast_button.click()
  time.sleep(60)
  </code></pre>
  
  <p>At that point I have a page that I can manually click "save as," and get a local file (with a corresponding folder of image/js assets) that lets me view the whole returned page locally (minus content which is generated dynamically from scrolling down the page, which is fine). I assumed there would be a simple way to mimic this 'save as' function in python/selenium but haven't found one. The code to save the page below just saves html, and does not leave me with a local file that looks like it does in the web browser, with images, etc.</p>
  
  <pre><code>content = driver.page_source
  with open('webpage.html', 'w') as f:
      f.write(content)
  </code></pre>
  
  <p>I've also found <a href="https://stackoverflow.com/a/14517464/1870832">this question/answer on SO</a>, but the accepted answer just brings up the 'save as' box, and does not provide a way to click it (as two commenters point out)</p>
  
  <p>Is there a simple way to 'save [full page] as' using python? Ideally I'd prefer an answer using selenium since selenium makes the crawling part so straightforward, but I'm open to using another library if there's a better tool for this job. Or maybe I just need to specify all of the images/tables I want to download in code, and there is no shortcut to emulating the right-click 'save as' functionality?</p>
  
  <p>UPDATE - Follow up question for James' answer
  So I ran James' code to generate a <code>page.html</code> (and associated files) and compared it to the html file I got from manually clicking save-as. The <code>page.html</code> saved via James' script is great and has everything I need, but when opened in a browser it also shows a lot of extra formatting text that's hidden in the manually save'd page. See attached screenshot (manually saved page on the left, script-saved page with extra formatting text shown on right). 
  <a href="https://i.stack.imgur.com/k4poh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/k4poh.png" alt="enter image description here"></a></p>
  
  <p>This is especially surprising to me because the raw html of the page saved by James' script seems to indicate those fields should still be hidden. See e.g. the html below, which appears the same in both files, but the text at issue only appears in the browser-rendered page on the one saved by James' script:</p>
  
  <pre><code>&lt;p class="helpbox ui-ncbitoggler-slave ui-ncbitoggler" id="hlp1" aria-hidden="true"&gt;
  These options control formatting of alignments in results pages. The
  default is HTML, but other formats (including plain text) are available.
  PSSM and PssmWithParameters are representations of Position Specific Scoring Matrices and are only available for PSI-BLAST. 
  The Advanced view option allows the database descriptions to be sorted by various indices in a table.
  &lt;/p&gt;
  </code></pre>
  
  <p>Any idea why this is happening?</p>