如何刷新已经打开的网页

如何刷新已经打开的网页

问题描述:

我只想用Selenium刷新一个已经打开的网页.

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

它总是打开一个新的浏览器窗口.

It always opens a new browser window.

我做错了什么?

from selenium import webdriver
import urllib
import urllib2

driver = webdriver.Firefox()
driver.refresh()

我建议将驱动程序元素搜索绑定到标签主体,并使用浏览器的刷新命令.

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

例如在OSX中

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

此处有关密钥的文档: http://selenium-python.readthedocs.org/en/latest/api.html

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 ?