How to open and close a website using default browser with python

2024/9/22 3:54:35

I'm trying to write a python script on windows platform to open a webpage(such as Google), and then, after 10 seconds, close this website.

Note: I'm using Windows 7, Python 2.7.10, and IE

Answer

You can use pythons built in webbrowser module to open the default browser:

import webbrowser
webbrowser.open("http://google.co.uk")

https://docs.python.org/2/library/webbrowser.html


If you want more control of the browser (for example the ability to close the browser) you could investigate the use of Selenium, however I believe you have to be specific about what browser to open.

from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("http://google.co.uk")
sleep(10)
driver.close()

http://selenium-python.readthedocs.org/en/latest/getting-started.html

https://en.xdnf.cn/q/71988.html

Related Q&A

Comparing numpy array with itself by element efficiently

I am performing a large number of these calculations:A == A[np.newaxis].Twhere A is a dense numpy array which frequently has common values.For benchmarking purposes we can use:n = 30000 A = np.random.r…

Kivy: BoxLayout vs. GridLayout

BoxLayout(orientation=vertical) vs. GridLayout(cols=1):They both do the same thing, no? Is there a reason to choose one over the other?

Flask circular dependency

I am developing a Flask application. It is still relatively small. I had only one app.py file, but because I needed to do database migrations, I divided it into 3 using this guide:https://realpython.co…

How to create tox.ini variables

Is there a way to set arbitrary variables within tox.ini?An example would be a project name that might be used in a variety of ways. With a rather complex tox.ini, I find myself copy and pasting all …

How to apply json_normalize on entire pandas column

I have a dataframe with LISTS(with dicts) as column values . My intention is to normalize entire column(all rows). I found way to normalize a single row . However, Im unable to apply the same function …

Configure Vs code version 2.0.0 Build Task for python

I need help in configuring my Vs code to run scripts in python using Cntrl Shift B, I was working fine until Vs code upgraded to version 2.0.0 now it wants me to configure the Build. And I am clueless…

Generate N-Grams from strings with pandas

I have a DataFrame df like this: Pattern String 101 hi, how are you? 104 what are you doing? 108 Python is good to learn.I want to crea…

Merge dataframes on multiple columns with fuzzy match in Python

I have two example dataframes as follows:df1 = pd.DataFrame({Name: {0: John, 1: Bob, 2: Shiela}, Degree: {0: Masters, 1: Graduate, 2: Graduate}, Age: {0: 27, 1: 23, 2: 21}}) df2 = pd.DataFrame({Name: {…

Prevent Celery Beat from running the same task

I have a scheduled celery running tasks every 30 seconds. I have one that runs as task daily, and another one that runs weekly on a user specified time and day of the week. It checks for the "star…

Tastypie with application/x-www-form-urlencoded

Im having a bit of difficulty figuring out what my next steps should be. I am using tastypie to create an API for my web application. From another application, specifically ifbyphone.com, I am receivin…