I've written a Python script running on a windows 32bit machine that will query a mysql database and then proceed to open a webbrowser with the relevant url and after 15 seconds it will terminate in a very crude way the browser process and it does this for each entry in the result set.
Is there a nicer way to load a page, wait for the page to fully load and then close the browser instead of using time.sleep()
and terminating the browser process so crudely?
import webbrowser,time,subprocess,pymysql
# open db connection
db = pymysql.connect(host="localhost", user="dev", passwd="password", db="testdb")
# create cursor and execure SQL statement
cursor = db.cursor()
cursor.execute("SELECT name, id FROM test_table")
# commit your changes if any
db.commit()
# get number of rows in resultset
numrows = int(cursor.rowcount)
# loop through resultset
for x in range(0, numrows):
row = cursor.fetchone()
url = ''
if (row[1] == 1):
url = 'http://localhost/?name=' + row[0]
if (row[1] == 2):
url = 'http://localhost/?func=2&name=' + row[0]
webbrowser.open_new(url)
time.sleep(15)
subprocess.call("taskkill /IM iexplore.exe /f /t", shell=True)
time.sleep(15)
cursor.close()
db.close()