13

I'm using Python to gather some information, construct a very simple html page, save it locally and display the page in my browser using webbrowser.open('file:///c:/testfile.html'). I check for new information every minute. If the information changes, I rewrite the local html file and would like to reload the displayed page.

The problem is that webbrowser.open opens a new tab in my browser every time I run it. How do I refresh the page rather than reopen it? I tried new=0, new=1 and new=2, but all do the same thing. Using controller() doesn't work any better.

I suppose I could add something like < META HTTP-EQUIV="refresh" CONTENT="60" > to the < head > section of the html page to trigger a refresh every minute whether or not the content changed, but would prefer finding a better way.

Exact time interval is not important.

Python 2.7.2, chrome 26.0.1410.64 m, Windows 7 64.

foosion
  • 7,619
  • 25
  • 65
  • 102
  • But, why do yo do in this way? Why dont you do AJAX or similar to read a chunk of data and parse then in your browser? with jQuery is simple, and dont have to regenerate always the webpage, only have to create a file and then show it with JQuery – ManuParra May 06 '13 at 13:37
  • How is AJAX and jQuery easier than adding < META HTTP-EQUIV="refresh" CONTENT="60" > to the generated html file, opening it once with webbbrowser, then letting it refresh every minute (either reading the old file or a newly generated one)? – foosion May 06 '13 at 17:26

8 Answers8

9

If you're going to need a refresh on the same tab, you'll need selenium webdriver. After installing selenium using pip, you can use the following code :

from selenium import webdriver
import time
import urllib
import urllib2
    
x = raw_input("Enter the URL")
refreshrate = raw_input("Enter the number of seconds")
refreshrate = int(refreshrate)
driver = webdriver.Firefox()
driver.get("http://"+x)

while True:
    time.sleep(refreshrate)
    driver.refresh()

This will open the URL and refresh the tab every refreshrate seconds

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
3

I use pyautogui module to refresh the browser page. It's one liner:

import pyautogui

pyautogui.hotkey('f5') #Simulates F5 key press = page refresh
StockB
  • 755
  • 1
  • 13
  • 33
PSP2019
  • 41
  • 2
1

Keep it very short, as simple as:

from selenium import webdriver
import time

driver = webdriver.Firefox()

driver.get('URL')
while True:
    time.sleep(20)
    driver.refresh()
driver.quit()
P_n
  • 940
  • 2
  • 11
  • 25
0

It looks like several people have asked this in the past but here is a link that sums it up.

Python refresh HTML document

But webbrowser.open( url, new=0 ) should open the page in the current window and not initialize a new one.

Community
  • 1
  • 1
Dan K
  • 409
  • 3
  • 12
  • 1
    new=0 should work, but it doesn't, at least not for that poster or for me, as mention in my question. That link recommends javascript, which seems like overkill. Meta-refresh would seem like the best alternative? – foosion May 06 '13 at 13:25
  • neither here. it opens up a new tab everytime. – Tony May 27 '18 at 20:48
0

The LivePage extension for Chrome. You can write to a file, then LivePage will monitor it for you. You can also optionally refresh on imported content like CSS. Chrome will require that you grant permissions on local file:// urls.

(I'm unaffiliated with the project.)

Reece
  • 7,616
  • 4
  • 30
  • 46
0

Or, you can use the easy auto refresh extension in chrome :D

It allows you to set your own refresh time, fastest being one second!

enter image description here

Aj463
  • 31
  • 2
0

I have had same issues as you for some reason new=1 , new=0 doesn't work. U may try the code below. It simply open the url, waits for 10 sec then kill the process n repeat.

    from subprocess import Popen
    import time

    while True:
       link = ('https://www.google.com/')
       Popen(['start', 'chrome', link], shell=True)
       time.sleep(10)
       Popen('taskkill /F /IM chrome.exe', shell=True)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '22 at 23:18
0

After using pip install to install pyautogui use the following:

import pyautogui
import time

for i in range(number of times you want to refresh):
    time.sleep(refreshrate in seconds)
    pyautogui.hotkey('f5')