最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selenium WebDriver Python reload html without refreshing the page - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Dec 11, 2016 at 14:20 ImportanceOfBeingErnest 340k60 gold badges732 silver badges763 bronze badges asked Dec 11, 2016 at 13:39 user92020user92020 1131 silver badge7 bronze badges 2
  • I dont think you could do such thing with Selenium. check this alternative. jmeter.apache – Amin Etesamian Commented Dec 11, 2016 at 14:09
  • @AminEtesamian Thanks, looks nice, but I need to use python. – user92020 Commented Dec 11, 2016 at 18:23
Add a ment  | 

1 Answer 1

Reset to default 4

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./a/39536091/6284629)

# this code would print the source of a page every second
from selenium import webdriver
from selenium.webdriver.mon.desired_capabilities import DesiredCapabilities
import time

# side note, how to get marionette working for firefox:
# https://stackoverflow./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)
发布评论

评论列表(0)

  1. 暂无评论