Python 如何刷新已经打开的网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28457515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh an already opened web page
提问by PhilippL
I just want to refresh an already opened web page with Selenium
.
我只想用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()
回答by Rupesh Shinde
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");
回答by aberna
I would suggest binding the driver element search to the tag body and use the refresh command of the browser.
我建议将驱动程序元素搜索绑定到标签正文并使用浏览器的刷新命令。
In OSX for example
例如在 OSX 中
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
此处的密钥文档: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 ?
您确定在刷新之前正确加载了带有驱动程序的网页吗?
回答by Sneakerhead Farb
The problem is you are opening the webdriver and then trying to refresh when you have not specified a URL.
问题是您正在打开 webdriver,然后在未指定 URL 时尝试刷新。
All you need to do is get your desired URL before refreshing:
您需要做的就是在刷新之前获取所需的 URL:
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()
回答by William Gao
I got mine fixed by adding "browser.refresh()" the for loop or while loop.
我通过在 for 循环或 while 循环中添加“browser.refresh()”来修复我的问题。
回答by Vicky
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
我使用 python 3.7.6,硒 3.141.0