Saving pages with Selenium

后端 未结 2 1128
自闭症患者
自闭症患者 2021-01-26 19:11

I will try again.

The code below I copied from another site and the user say it works (shows a screenshot).Original code

I tested the code: No error, but no fil

相关标签:
2条回答
  • 2021-01-26 19:39
    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Selenium\chromedriver.exe")
    
    driver.get("http://www.example.com")
    with open('page.html', 'w+') as f:
        f.write(driver.page_source)
        f.close()
    

    Must work

    0 讨论(0)
  • 2021-01-26 19:40

    If you do the key combination in the browser, you will see this only brings up the 'save page' dialog box. You need to additionally send ALT+S to save the page, in Windows it will be saved in your Downloads folder by default.

    saveas = ActionChains(driver).key_down(Keys.CONTROL).send_keys('S').key_up(Keys.CONTROL).send_keys('MyDocumentName').key_down(Keys.ALT).send_keys('S').key_up(Keys.ALT)
    

    EDIT: ActionChains are unreliable. It would be easier not to interact with the browser GUI.

    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Selenium\chromedriver.exe")
    
    driver.get("http://www.example.com")
    with open('page.html', 'w') as f:
        f.write(driver.page_source)
    
    0 讨论(0)
提交回复
热议问题