Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

google chrome - What is the fastest way to open urls in new tabs via Selenium - Python?

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

enter image description here

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Upgrade the chromedriver(>2.25)/chrome browser(>55.0) on your MAC to remove the empty data; tab. You can use threading or multiprocessing to speed up the process.

from multiprocessing import Process
import os
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver =webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("http://msn.com")
def func1():
  print 'launching: MSN'
 driver.execute_script("window.open('http://www.msn.com');")

def func2():
  print 'launching: CNN'
  driver.execute_script("window.open('http://www.cnn.com');")

if __name__ == '__main__':
  p1 = Process(target=func1)
  p1.start()
  p2 = Process(target=func2)
  p2.start()
  p1.join()
  p2.join()

def runInParallel(*fns):
  proc = []
  for fn in fns:
    p = Process(target=fn)
    p.start()
    proc.append(p)
  for p in proc:
    p.join()

runInParallel(func1, func2)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...