Python - Selenium - How to use Browser Shortcuts

后端 未结 4 1159
遥遥无期
遥遥无期 2020-12-19 08:51

Once a browser page is loaded I\'m looking to use the CRTL+P shortcut in Goggle Chrome to enter the print page and then simply press return to print the page.



        
4条回答
  •  一整个雨季
    2020-12-19 09:28

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    ActionChains(browser).send_keys(Keys.CONTROL, "p").perform()
    

    that would send the keyboard shortcut for a print dialog

    I haven't found a way to do this in FF for printing though - ctrl+p will open the print dialog, but FF has a focus bug that doesn't allow one to do Keys.ENTER for the dialog itself

    hopefully this will work for you in Chrome, I haven't tested it there

    please update if you find a way around this - possibly try AutoIt

    If none of the above works you can always do

    browser.get_screenshot_as_file( path + 'page_image.jpg' )
    

提交回复
热议问题