47

I just want to refresh an already opened web page with Selenium.

It always opens a new browser window.

What I'm doing wrong?

from selenium import webdriver
import urllib
import urllib2

driver = webdriver.Firefox()
driver.refresh()
2
  • 7
    Have you tried using driver.navigate().refresh()? Commented Feb 11, 2015 at 15:03
  • 1
    The code you show does not reproduce the problem you are reporting. The line webdriver.Frirefox() will start a new browser. But driver.refresh() does not do anything with the code you show. If you add a driver.get() call to load a good URL, then the page refreshes as it should.
    – Louis
    Commented Feb 11, 2015 at 17:54

7 Answers 7

53

I would suggest binding the driver element search to the tag body and use the refresh command of the browser.

In OSX for example

driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'r')

Documentation on keys here: http://selenium-python.readthedocs.org/en/latest/api.html

Update: The following code, very similar to your one, works fine for me.

    driver = webdriver.Firefox()
    driver.get(response.url) #tested in combination with scrapy   
    time.sleep(3)   
    driver.refresh()

Are you sure you correctly load the web page with the driver before refreshing it ?

1
  • 1
    Even in OSX, driver.refresh() works good. The first one with CMD + r didn't.
    – Nabin
    Commented Jun 7, 2020 at 8:15
16

The problem is you are opening the webdriver and then trying to refresh when you have not specified a URL.

All you need to do is get your desired URL before refreshing:

from selenium import webdriver
import urllib
import urllib2
driver = webdriver.Firefox()
driver.get("Your desired URL goes here...")
#now you can refresh the page!
driver.refresh()
4

The following codes work for me

driver.get(driver.current_url)
sleep(2)
driver.refresh()

I use python 3.7.6, selenium 3.141.0

3

You are trying to refresh the page before it loads so u can use a sleep function

from time import sleep
sleep(1)

or you can wait for an XPath to load so

WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath goes here)))
0

For me helped

from selenium import webdriver
import time


driver = webdriver.Firefox()
driver.get("URL")

time.sleep(5)
driver.refresh()
-1

You can try any one of the below methods for the same.

Method 1:

driver.findElement(By.name("s")).sendKeys(Keys.F5);

Method 2:

driver.get(driver.getCurrentUrl());

Method3:

driver.navigate().to(driver.getCurrentUrl());

Method4:

driver.findElement(By.name("s")).sendKeys("\uE035");
2
  • 11
    This does not pertain to python, which is the language tagged Commented Jun 23, 2017 at 22:09
  • 1
    That's not python
    – bntzio
    Commented Jan 19, 2018 at 14:57
-1

I got mine fixed by adding "browser.refresh()" the for loop or while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.