通过Selenium - Python打开新标签页的最快方式是什么? [英] What is the fastest way to open urls in new tabs via 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
我现在所拥有的是最快的方法是什么? 我曾经拥有这个 它可以工作,但很痛苦 现在我从6开始,但是我想加载100个标签。 另外,我该如何摆脱我的第一个奇怪的外观选项卡? 所以我们已经有两种方法可以打开 使用 当前使用 现在,您看到 我们尚未调用 为了让 要打开 打开 I want to create a python script to open up a lot tabs When I run I get Is what I have right now is the fastest way? I used to have this It works but it is painfully 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 Your previous approach with Your current approach with Now, the reason you see a After this, we haven't invoked the To bring the
To open a To open a
这篇关于通过Selenium - Python打开新标签页的最快方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
# - * - 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
。
我甚至确定为什么它在那里。 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'););
带网址的新标签$ c $您可以使用以下代码行:
driver.execute_script(window.open('http ://facebook.com/'););
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()
# -*- 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")
slow
.
New TAB
with Selenium
.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.execute_script
is bound to be faster as we are simply injecting Java Scripts
to open new TAB
s with URL
s.Blank Window
is because once you initialized the browser through :driver =webdriver.Chrome('/usr/local/bin/chromedriver')
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 :
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 :
New Blank TAB
you can use the following line of code : driver.execute_script("window.open('','_blank');");
New TAB with url
you can use the following line of code :driver.execute_script("window.open('http://facebook.com/');");