use*_*020 5 javascript python selenium page-refresh
我有一个具有自刷新内容的页面(通过 WebSocket),如下所示。虽然内容不断变化,但我的 Firefox Webdriver 只能看到初始内容。我可以通过刷新页面来获取最新的
driver.navigate.refresh()
Run Code Online (Sandbox Code Playgroud)
但这会导致不必要的流量,除了 Firefox 窗口中已经出现的新内容之外。
我的问题是:我能否获得在 Firefox 窗口中观察到的新鲜 html,而无需重新加载整个页面?
如果页面内容在一段时间内发生变化,您可以做的一个选择是每 n 秒检查一次页面源。一个简单的方法是import time等待time.sleep(5)5 秒,然后获取页面源代码。你也可以把它放在一个循环中,如果页面内容在接下来的5秒内发生了变化,那么selenium在检查时应该能够获取到更新后的页面内容。我还没有测试过这个,但请随意检查它是否适合您。
编辑:添加示例代码。确保您已正确安装和配置 Marionette。如果您是 ubuntu 用户,您可以在这里查看我的答案(/sf/answers/2767526401/)
# this code would print the source of a page every second
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
# side note, how to get marionette working for firefox:
# /sf/answers/2767526401/
capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = True
browser = webdriver.Firefox(capabilities=capabilities)
# load the page
browser.get("http://url-to-the-site.xyz")
while True:
# print the page source
print(browser.page_source)
# wait for one second before looping to print the source again
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5476 次 |
| 最近记录: |