3

I am trying to use CTRL + S in selenium to save contents of a page but can't get anything happening. If I try to do it using my keyboard the save window opens.

from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.maximize_window()
action_chains = ActionChains(driver)
options = webdriver.ChromeOptions() 

options.add_argument("download.default_directory=C:/Downloads, download.prompt_for_download=False")

driver = webdriver.Chrome(options=options)


driver.get("https://imagecyborg.com/")

action_chains.send_keys(Keys.CONTROL).send_keys("s").perform()
user2419259
  • 93
  • 1
  • 8
  • 1
    Does this answer work? https://stackoverflow.com/questions/52142180/saving-pages-with-selenium – mermaldad Dec 05 '19 at 13:37
  • If you try with key_down(value, element=None)[source] https://selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html – GiovaniSalazar Dec 05 '19 at 13:44
  • Tried changing to key down and getting the same – user2419259 Dec 05 '19 at 14:05
  • Look at mermaldad's answer. If that doesn't work for you, depending on what you're trying to do you could programmatically save the dom, or use browsermob proxy to capture the request contents (which is NOT going to be the same as the fully rendered page of course) – DMart Dec 05 '19 at 16:14
  • His link doesnt have a solution. To be more specific I have a link as follows: s3.amazon.com/bucketname/1233. Note the link doesnt have the file name or extensions. Only when I right click and save I am able to know the type. Some links are jpg, some are webp, others are video. In some cases you can t see page source to get the source url. So only way left was to automate the ctrl s which isnt working – user2419259 Dec 05 '19 at 16:29

1 Answers1

4

The only thing that worked for me was pyautogui:

import pyautogui
pyautogui.hotkey('ctrl','s')
pyautogui.press('enter')
TaskaPaska
  • 41
  • 3
  • I had to add a time.sleep() (page takes a while toload) but this does work for me :) – steveman May 11 '21 at 16:01
  • @steveman, do you still have this code? – punky Dec 07 '22 at 21:39
  • @punky I just now read your question ffprofile = webdriver.FirefoxProfile( "") driver = webdriver.Firefox(firefox_profile=ffprofile) driver.get() pyautogui.hotkey('ctrl', 's') sleep(3) #the sleep I mentioned here pyautogui.press('enter') – steveman Dec 22 '22 at 13:13