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!

10
  • Be more specific about what combination do you want to send. Commented Feb 13, 2021 at 22:52
  • @bilakos control+shift+r to hard refresh in chrome Commented Feb 13, 2021 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. Commented Feb 13, 2021 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. Commented Feb 13, 2021 at 23:00
  • With pyautogui you can add this code pyautogui.hotkey('ctrl', 'shift', 'r') and it will make your combination Commented Feb 13, 2021 at 23:02

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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("[email protected]")

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

2 Comments

This was not what the author wants to know
I understood that already but thanks anyways!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.