如何使用 cmd/Python 关闭 Internet 选项卡? [英] How to close an internet tab with cmd/Python?
问题描述
所以我正在编写一个 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/')
在我打开它们之后:
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)])
如果你想打开一个带有三个标签的浏览器然后关闭我会使用 selenium 或类似的东西:
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()
这篇关于如何使用 cmd/Python 关闭 Internet 选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!