python - 如何使用 selenium python 打开一个新标签?

标签 python selenium

我正在尝试制作一个可以打开多个网站的程序,但我无法按下 control-t。我尝试了多种解决方案,但我找不到一个可行的。当我执行 keydown 方法时,我收到一个错误提示

webdriver has no attribute key_down

当我尝试 send_keys(Keys.CONTROL + 't') 时,它不会引发任何错误,也不会做任何事情。如何打开新标签页?

这是我的尝试:

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

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://youtube.com")
search = driver.find_element_by_id("search")
#search.keydown(Keys.CONTROL)

#Webelement.key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()

search.send_keys(Keys.CONTROL+'t')
time.sleep(10)

最佳答案

你可以这样做

from selenium import webdriver

driver.get("https://www.youtube.com")
search = driver.find_element_by_id("search")

driver.execute_script("window.open('https://www.google.com')")

关于python - 如何使用 selenium python 打开一个新标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68660642/

相关文章:

python - 如何在Python中启用辅助轴(twiny)的共享

python - 在函数中动态创建Python子类

selenium - 将 Selenium/Webdriver/HtmlUnit 限制到某个域

linux - Selenium Maven 项目与 jenkins 集成在 windows 上工作,但同一个项目在 linux 上出错

python - 使用 selenium 从 textarea 清除文本

python - 在 Selenium WebDriver 和 Python 中单击 JavaScript 链接

Python 3 错误 : 'module' object has no attribute 'split'

python - Tornado/python 中的 if else 语句

python - Pandas .unique() TypeError : unhashable type: 'list'

python - 将YouTube channel 名称纳入评论