问题
I have a page with self-refreshing content (via WebSocket) like this one. While the content is constantly changing my firefox webdriver can only see the initial content. I could get the fresh one by refreshing the page by
driver.navigate.refresh()
but this causes unnecessary traffic besides in the Firefox window the new content already appear.
My question is: Can I get the fresh html as I can observe in the Firefox window without reloading the whole page?
回答1:
If the page contents change over a period of time, one option you could do is check the page source every n seconds. A simple way to do this would be to import time
then use time.sleep(5)
to wait for 5 seconds, then get the page source. You can also put it in a loop, and if the page contents have changed within the succeeding 5 second periods, then selenium should be able to get the updated page contents when you check. I haven't tested this, but feel free to check if it works for you.
EDIT: Added sample code. Make sure that you have marionette properly installed and configured. You can check my answer here if you are an ubuntu user (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)
来源:https://stackoverflow.com/questions/41086773/selenium-webdriver-python-reload-html-without-refreshing-the-page