How to type F5 to refresh a page using Playwright Python
Question:
I’m trying to refresh a webpage using F5 key. I know I can use:
self.page.reload()
But this is not a good solution for my problem. How to make the page to be refreshed using the F5
key? My code doesn’t refresh the page and I don’t know why.
self.page.keyboard.press('F5')
Answers:
As you said we have page.keyboard.press('F5')
. But it does not do what you want, I’ve tried several other examples but nothing.
Maybe this is enough for you?
page.evaluate('window.location.reload();')
Or
page.evaluate('location.reload();')
As you can see we are forcing the reload by evaluating a javascript.
Yes…it is a little bit tricky, so probably you will need to even force a time.sleep(2)
after that, and trust me, I hate time.sleep()
but this is an special situation.
I hope it helps.
Try to make it a bit slower like a human does by injecting delay parameter as follows:
self.page.keyboard.press('F5',delay=100)
I’m trying to refresh a webpage using F5 key. I know I can use:
self.page.reload()
But this is not a good solution for my problem. How to make the page to be refreshed using the F5
key? My code doesn’t refresh the page and I don’t know why.
self.page.keyboard.press('F5')
As you said we have page.keyboard.press('F5')
. But it does not do what you want, I’ve tried several other examples but nothing.
Maybe this is enough for you?
page.evaluate('window.location.reload();')
Or
page.evaluate('location.reload();')
As you can see we are forcing the reload by evaluating a javascript.
Yes…it is a little bit tricky, so probably you will need to even force a time.sleep(2)
after that, and trust me, I hate time.sleep()
but this is an special situation.
I hope it helps.
Try to make it a bit slower like a human does by injecting delay parameter as follows:
self.page.keyboard.press('F5',delay=100)