我有一个自我刷新内容的页面(通过WebSocket),像这样one。虽然内容在不断变化,但我的firefox webdriver只能看到最初的内容。我可以通过刷新页面得到新鲜的页面Selenium WebDriver Python重新加载html而无需刷新页面
driver.navigate.refresh()
但是这会导致不必要的流量,除了在Firefox窗口中新的内容已经出现。
我的问题是:我可以得到新的HTML,因为我可以在Firefox窗口中观察,而无需重新加载整个页面?
我有一个自我刷新内容的页面(通过WebSocket),像这样one。虽然内容在不断变化,但我的firefox webdriver只能看到最初的内容。我可以通过刷新页面得到新鲜的页面Selenium WebDriver Python重新加载html而无需刷新页面
driver.navigate.refresh()
但是这会导致不必要的流量,除了在Firefox窗口中新的内容已经出现。
我的问题是:我可以得到新的HTML,因为我可以在Firefox窗口中观察,而无需重新加载整个页面?
如果页面内容在一段时间内发生变化,您可以做的一个选项是每n秒检查页面源。一个简单的方法是import time
,然后使用time.sleep(5)
等待5秒钟,然后获取页面源代码。你也可以把它放在一个循环中,如果页面内容在接下来的5秒内发生了变化,那么硒在检查时应该能够获得更新的页面内容。我没有测试过,但可以随时检查它是否适用于您。
编辑:增加了示例代码。确保您有木偶正确安装和配置。你可以在这里查看我的回答如果你是Ubuntu的用户(https://stackoverflow.com/a/39536091/6284629)
# 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:
# https://stackoverflow.com/a/39536091/6284629
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)
我不认为你可以做这样的事情硒。检查这个选择。 http://jmeter.apache.org – Juggernaut
@AminEtesamian谢谢,看起来不错,但我需要使用python。 – user92020