I want to create a python script to open up a lot tabs
import os
import selenium
from selenium import webdriver
driver =webdriver.Chrome('/usr/local/bin/chromedriver')
driver.execute_script("window.open('http://www.msn.com');")
driver.execute_script("window.open('http://www.cnn.com');")
driver.execute_script("window.open('http://www.yahoo.com');")
driver.execute_script("window.open('https://www.apple.com');")
driver.execute_script("window.open('https://www.google.com');")
driver.execute_script("window.open('https://www.stackoverflow.com');")
# driver.quit()
When I run I get
Is what I have right now is the fastest way?
I used to have this
# -*- coding: utf-8 -*-
import os
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver =webdriver.Chrome('/usr/local/bin/chromedriver')
#1
driver.get("http://msn.com")
#2
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.switch_to.window(driver.window_handles[-1])
driver.get("http://cnn.com")
#3
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.switch_to.window(driver.window_handles[-1])
driver.get("http://www.yahoo.com")
#4
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.switch_to.window(driver.window_handles[-1])
driver.get("https://www.apple.com")
#5
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.switch_to.window(driver.window_handles[-1])
driver.get("https://www.google.com")
#6
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.switch_to.window(driver.window_handles[-1])
driver.get("https://www.stackoverflow.com")
It works but it is painfully slow
.
I start with 6 now, but I want to load 100 tabs.
Also, how do I get rid of my first weird looking tab? I am even sure why it is there.
So we already have have 2 approaches at out disposal to open
New TAB
withSelenium
.Your previous approach with
send_keys(Keys.COMMAND + 't')
is bound to consume more time as weswitch_to.window()
andget("http://www.url.com")
too. Hence slower.Your current approach with
execute_script
is bound to be faster as we are simply injectingJava Scripts
to open newTAB
s withURL
s.Now, the reason you see a
Blank Window
is because once you initialized the browser through :After this, we haven't invoked the
get()
method to open anyURL
. Rather we have straight taken help ofJavascriptExecutor
to openNEW TAB
s. Hence the firstTAB
remains unused.Update :
To bring the
unused TAB
into the play, we can access the first of theURL
s through the firstTAB
invokingget()
method as follows :Summary :
To open a
New Blank TAB
you can use the following line of code :To open a
New TAB with url
you can use the following line of code :Upgrade the chromedriver(>2.25)/chrome browser(>55.0) on your MAC to remove the empty data; tab. You can use
threading
ormultiprocessing
to speed up the process.Depending on how many CPUs you have, the load of the machine, the timing of many things happening in the computer will have an influence on the time the threads/process start. Also, since the processes are started right after creation, the overhead of creating a process also has to be calculated in the time difference you see.