How to refresh an already opened web page

2019-06-16 01: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()

3条回答
走好不送
2楼-- · 2019-06-16 02:15

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()
查看更多
Emotional °昔
3楼-- · 2019-06-16 02:22

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 ?

查看更多
小情绪 Triste *
4楼-- · 2019-06-16 02:27

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");
查看更多
登录 后发表回答