如何使用默认浏览器和python打开和关闭网站 [英] How to open and close a website using default browser with python

查看:105
本文介绍了如何使用默认浏览器和python打开和关闭网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Windows平台上编写python脚本来打开网页(例如Google),然后在10秒钟后关闭此网站.

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.

注意:我正在使用Windows 7,Python 2.7.10和IE

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

推荐答案

您可以使用内置在webbrowser模块中的python来打开默认浏览器:

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

如果您想进一步控制浏览器(例如关闭浏览器的功能),可以调查 Selenium的使用. ,但是我相信您必须具体说明要打开的浏览器.

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

这篇关于如何使用默认浏览器和python打开和关闭网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆