简体   繁体   English

Selenium webdriver在python中的chrome编码中打开一个新选项卡

[英]Selenium webdriver open a new tab in chrome coding in python

I am developing a program that opens a web driver.我正在开发一个打开网络驱动程序的程序。 Now I want to open that driver and after opening it should open a new chrome tab with a link.现在我想打开该驱动程序,打开它后应该打开一个带有链接的新 chrome 选项卡。 How can I do that?我怎样才能做到这一点? Please help请帮忙

            chrome_options = webdriver.ChromeOptions()
            driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
            driver.get(url)

I am using this to open the driver (url= is a variable from above) after opening want that it opens a new chrome tab please do that!我正在使用它来打开驱动程序(url= 是上面的变量)打开后希望它打开一个新的 chrome 标签,请这样做!

One way to do that is the following:一种方法是:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

BASE_URL = "https://www.google.com/" # your url

driver = webdriver.Chrome(
        executable_path=ChromeDriverManager().install()
    )
driver.get(BASE_URL)
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get(BASE_URL)

You spawn a new window, switch to the window and perform a new get.您生成一个新窗口,切换到该窗口并执行新的获取。

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

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