1

I am Software student.

I was messing around with selenium in python in chrome, and I'm trying to make a auto purchase bot. But one thing doesn't really want to work. I would like to hard refresh, and I was curious if there is a way to send a key combination Ctrl + Shift + r to the driver. How can we do this?

Thank you!

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
  • Be more specific about what combination do you want to send. – bilakos Feb 13 '21 at 22:52
  • @bilakos control+shift+r to hard refresh in chrome – Itz_Floris Feb 13 '21 at 22:56
  • @Chris not really i used actions.key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys('r').key_up(Keys.CONTROL).key_up(Keys.SHIFT).perform() but that didnt work. – Itz_Floris Feb 13 '21 at 22:59
  • You have 2 ways to do that. The first is to `refresh` the chrome from the Chrome driver. You have mentioned a variable like `driver` or `browser`. So go there and type `driver.refresh()` or `browser.refresh()` or the variable you have mentioned. The second method is by importing the module `keyboard`. Go to the `cmd` and type `pip install keyboard` or by importing `puautogui` and you can install it like this `pip install pyautogui`. After that import it on the code you have wrote. – bilakos Feb 13 '21 at 23:00
  • With `pyautogui` you can add this code `pyautogui.hotkey('ctrl', 'shift', 'r')` and it will make your combination – bilakos Feb 13 '21 at 23:02
  • @bilakos So i have i use browser.refresh() but its not a hard refresh and with something like keyboard its like a keypress on my hole computer, I want that only my driver gets a send_key so i can do other stuff while thats running in the back. – Itz_Floris Feb 13 '21 at 23:07
  • @Itz_Floris What do you mean by saying "hard refresh" ? – bilakos Feb 13 '21 at 23:08
  • @bilakos so it clears my browser cache for the specific page – Itz_Floris Feb 13 '21 at 23:11
  • @Itz_Floris try this `browser.execute_script("location.reload(true);")` – bilakos Feb 13 '21 at 23:13
  • it won't invalidate all cached contents it will invalidates only main page use driver.execute_cdp_cmd("Page.reload", {"ignoreCache": True}) devprotocol . Added it to answer – PDHide Feb 14 '21 at 00:07

2 Answers2

0

To send keys to driver you first need to specify the html element you are sending keys to.

emailBox = driver.find_element_by_xpath("xpath here")
emailBox.send_keys("example@example.com")

And repeat for every element. You can also use the .click() function for buttons.

0

ctrl+shift+r is a browser level short cut you cannot trigger that from selenium.

If you want to refresh by forcing the pages to be loaded from server and not cache use the devtool protocal:

First install Selenium 4 :

pip install selenium==4.0.0.a7

Selenium 4 supports devtool protocol now use below code:

 driver.get(
        "https://stackoverflow.com/questions/66190723/python-selenium-send-keys-to-driver")
 driver.execute_cdp_cmd("Page.reload", {"ignoreCache": True})

https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-reload

Page.reload # Reloads given page optionally ignoring the cache. PARAMETERS ignoreCache boolean If true, browser cache is ignored (as if the user pressed Shift+refresh). scriptToEvaluateOnLoad string If set, the script will be injected into all frames of the inspected page after reload. Argument will be ignored if reloading dataURL origin.

Updated on the Comment

location.reload(boolean) is deprecated now and also:

location.reload() as per the comment may not validate all the cached resources:

https://www.chromestatus.com/feature/5724282256621568

creed
  • 1,409
  • 1
  • 9
  • 18
PDHide
  • 18,113
  • 2
  • 31
  • 46