繁体   English   中英

Python - 如何打开多个浏览器窗口

[英]Python - How can i open multiple browser windows

我是 python 新手,我将使用下面的代码打开浏览器窗口并做一些事情。 但是,当我同时打开多个 URL 时,它只会在现有浏览器窗口中打开一个新选项卡,但我希望它在新窗口中打开,然后在新窗口上打开更多选项卡。 这是代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import webbrowser

#path for the driver
driver  = webdriver.Chrome(executable_path="C:\mydriver\chromedriver")

driver.get("https://www.google.com")
driver.execute_script("window.open ('https://www.google.com', 'new window')")
driver.switch_to.window(driver.window_handles[0])

driver.execute_script("window.open ('https://www.bing.com','https://www.facebook.com', 'new window')")
driver.switch_to.window(driver.window_handles[1])

尝试这个:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import webbrowser

#path for the driver
driver  = webdriver.Chrome(executable_path="C:\mydriver\chromedriver")

driver.get("https://www.google.com")
driver.execute_script("window.open ('https://www.google.com', 'new window')")
driver.switch_to.window(driver.window_handles[0])

for page in ('https://www.bing.com','https://www.facebook.com'):
    driver.execute_script(f"window.open ('{page}')")
driver.switch_to.window(driver.window_handles[1])

将其插入您要打开的位置并切换到新选项卡。 driver.execute_script("window.open('https://website.com')") driver.switch_to.window(driver.window_handles[TAB])

将 TAB 变量替换为您要切换到的选项卡(例如,如果您打开一个新选项卡,请在 TAB 变量的位置输入“2”。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM