如何使用cmd / Python关闭Internet选项卡? [英] How to close an internet tab with cmd/Python?

查看:82
本文介绍了如何使用cmd / Python关闭Internet选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写Python脚本以使用cmd打开互联网链接。例如:

So I'm writing a Python script to open internet links using cmd. For example:

import os
os.system('start http://stackoverflow.com/')
os.system('start http://www.google.com/')
os.system('start http://www.facebook.com/')

打开它们后,我会这样做:

After I open them I do:

import time 
time.sleep(60)

所以我可以等一下在做其他事情之前。在打开这些标签60秒钟后,似乎无法找到任何方法关闭这些标签?是否可以使用命令关闭cmd中的Internet选项卡?

So I can wait a minute before doing anything else. What I can't seem to find anywhere is a way to close these tabs after I have opened them for 60 seconds? Is there a command I can use to close internet tabs in cmd?

注意:我使用的是 Windows 8 Python 2.7.9 Google Chrome

Note: I'm using Windows 8, Python 2.7.9, and Google Chrome

推荐答案

您可以启动进程,然后在60秒后将其杀死。

You can start processes and then kill them after 60 seconds.

from subprocess import Popen, check_call

p1 = Popen('start http://stackoverflow.com/')
p2 = Popen('start http://www.google.com/')
p3 = Popen('start http://www.facebook.com/')

time.sleep(60)
for pid in [p1.pid,p2.pid,p3.pid]:
    check_call(['taskkill', '/F', '/T', '/PID', str(pid)])

替换系统

如果您要打开浏览器带有三个选项卡,然后关闭,我将使用硒或类似的东西:

If you want to open a browser with three tabs then close I would use selenium or something similar:

import time
from selenium import webdriver


dr = webdriver.Chrome()

dr.get('http://stackoverflow.com/')
dr.execute_script("$(window.open('http://www.google.com/'))")
dr.execute_script("$(window.open('http://facebook.com/'))")

time.sleep(5)
dr.close()
dr.switch_to.window(dr.window_handles[-1])
dr.close()
dr.switch_to.window(dr.window_handles[-1])
dr.close()

chromedriver

这篇关于如何使用cmd / Python关闭Internet选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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