通过Selenium - Python打开新标签页的最快方式是什么? [英] What is the fastest way to open urls in new tabs via Selenium - Python?

查看:442
本文介绍了通过Selenium - Python打开新标签页的最快方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个python脚本来打开很多标签

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

当我运行I get





我现在所拥有的是最快的方法是什么?



我曾经拥有这个

 # -  *  -  coding:utf- 8  -  *  -  

导入os
从selenium导入selenium
导入webdriver
从selenium.webdriver.common.keys导入键

驱动程序= 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])$ ​​b $ b 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])$ ​​b $ b driver.get(http://www.yahoo.com)

#4
驱动程序send_keys(Keys.COMMAND +'t')
driver.switch_to.window(driver.window_handles [-1])$ ​​b $ b 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])$ ​​b $ b 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])$ ​​b $ b driver.get (https://www.stackoverflow.com)

它可以工作,但很痛苦 slow






现在我从6开始,但是我想加载100个标签。



另外,我该如何摆脱我的第一个奇怪的外观选项卡?
我甚至确定为什么它在那里。

解决方案

所以我们已经有两种方法可以打开 New TAB Selenium

使用 send_keys(Keys.COMMAND +'t') 必然会消耗更多时间,因为我们 switch_to。 window() get(http://www.url.com) 。因此,较慢。



当前使用 execute_script 的方法肯定会更快,因为我们只是简单地注入 Java Scripts 来打开新的 TAB s与 URL s。



现在,您看到 空白窗口 是因为一旦您通过以下方式初始化浏览器:

  driver = webdriver.Chrome('/ usr / local / bin / chromedriver')

我们尚未调用 get() 方法来打开任何 URL 即可。我们直接接受了 JavascriptExecutor 的帮助来打开 NEW TAB TAB 未被使用。 b
$ b




更新:



为了让未使用的TAB 进场,我们可以访问第一个<通过第一个 TAB 调用 get()方法将code> URL 如下:

$ p $ driver = webdriver.Chrome('/ usr / local / bin / chromedriver')
driver.get (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.execute_script(window.open('','_ blank');); 

打开 带网址的新标签

  driver.execute_script(window.open('http ://facebook.com/');); 


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 with Selenium.

Your previous approach with send_keys(Keys.COMMAND + 't') is bound to consume more time as we switch_to.window() and get("http://www.url.com") too. Hence slower.

Your current approach with execute_script is bound to be faster as we are simply injecting Java Scripts to open new TAB s with URL s.

Now, the reason you see a Blank Window is because once you initialized the browser through :

driver =webdriver.Chrome('/usr/local/bin/chromedriver')

After this, we haven't invoked the get() method to open any URL. Rather we have straight taken help of JavascriptExecutor to open NEW TAB s. Hence the first TAB remains unused.


Update :

To bring the unused TAB into the play, we can access the first of the URL s through the first TAB invoking get() method as follows :

driver =webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("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');")


Summary :

To open a New Blank TAB you can use the following line of code :

 driver.execute_script("window.open('','_blank');");

To open a New TAB with url you can use the following line of code :

driver.execute_script("window.open('http://facebook.com/');");

这篇关于通过Selenium - Python打开新标签页的最快方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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