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

5

6

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

Decay answered 27/11, 2015 at 9:7 Comment(0)
I
14

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 sleep

driver = webdriver.Firefox()
driver.get("http://google.co.uk")
sleep(10)
driver.close()

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

Illustrational answered 27/11, 2015 at 10:0 Comment(0)
W
0

The Best place to start web interaction in python is Mechanize .

import mechanize
br = mechanize.Browser()
br.open("http://www.example.com/")

or you can use urllib https://docs.python.org/2/howto/urllib2.html

import urllib2
page = urllib2.urlopen("http://example.com/").read()
print page
Whenever answered 27/11, 2015 at 9:27 Comment(2)
How would you close?Fusil
<response_seek_wrapper at 0x1189a2710 whose wrapped object = <closeable_response at 0x1189a3f10 whose fp = <_io.BufferedReader name=65>>>Terraterrace
U
0

create a subprocess and then close using the process handle

import time
import subprocess

p = subprocess.Popen(["firefox", "http://www.google.com"])
time.sleep(10) #delay of 10 seconds
p.kill()
Unarm answered 27/11, 2015 at 9:28 Comment(1)
You do not know if a user actually uses firefox. You should let the system handle the "url call".Searby
S
0

i am working on ubuntu 16.04 and i solve this problem by using geckodriver.exe file.these steps are very easy,please read carefully.

:: at first you have to install selenium by typing this command on terminal>>

        for python2:-  python -m pip install --user selenium
        for python3:-  python3 -m pip install --user selenium

:: next step download geckodriver using link given below >>

       https://github.com/mozilla/geckodriver/releases

:: since i am using ubuntu so i download geckodriver-v0.24.0-linux64.tar.gz
now extract it.

:: now in the python code for firefox browsing add these lines >>

 from selenium import webdriver

 browser = webdriver.Firefox(executable_path = '/home/aman/Downloads/geckodriver')
 browser.get('https://www.google.com')
 browser.close()

::for chrome browser >>

 from selenium import webdriver
 browser = webdriver.chrome(executable_path = '/home/aman/Downloads/geckodriver')
 browser.get('https://www.google.com')
 browser.close()

:: in my pc i extract geckodriver in /home/aman/Downloads/geckodriver so you have to put whole path for geckodriver file where you extract your file.

:: now run this python file, i hope this will definitely work for you.

Stealer answered 1/4, 2019 at 10:49 Comment(1)
erroe- SessionNotCreatedException: Message: Unable to find a matching set of capabilitiesTerraterrace
A
0

Maybe try this:

import time
import webbrowser
import os

url = 'https://i.pinimg.com/originals/66/b5/5f/66b55f8e2ca22a800af0aecf9d01d848.gif'

def function(test):
    x = webbrowser.open(url)

    while x != 6:
        x = webbrowser.open(url)       
        time.sleep(2)
        os.system('taskkill /im chrome.exe /f')

function('test')
Agan answered 4/6, 2021 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.