How to close a tab on the web using cmd / Python?

So, I am writing a Python script to open internet links using cmd. For instance:

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)

So I can wait a minute before doing anything else. What I cannot find is a way to close these tabs after I open them for 60 seconds? Is there a command that I can use to close Internet tabs in cmd?

Note. I am using Windows 8 , Python 2.7.9 and Google Chrome

+4
source share
3 answers

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)])

replacing-os-system

, - :

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, selenium

+5

Chrome . chrome.exe <url> . chrome chrome . PID chrome.exe , ( ).

"":

  • chrome.exe <url>
  • 60
  • chrome.exe

, :

from subprocess import Popen
import time

urls = ['http://www.facebook.com/', 'http://www.google.com/']

for url in urls:
    Popen(['start', 'chrome' , url], shell=True)

time.sleep(60)

Popen('taskkill /F /IM chrome.exe', shell=True)

- chrome

+1
from selenium import webdriver  
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()
browser.get('http://stackoverflow.com')
main_window = browser.current_window_handle

browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
browser.get('http://stackoverflow.com/questions/tagged/django')

10 .

time.sleep(10)

Close a new tab. The first tab remains open.

browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
0
source

All Articles