gpt4 book ai didi

python - 使用 ActionChains 在 Selenium Webdriver 中按 ctrl+t 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:59 24 4
gpt4 key购买 nike

我需要在测试中打开一个新的浏览器选项卡,我了解到最好的方法是简单地将适当的键发送到浏览器。我使用的是 Windows,所以我使用 ActionChains(driver).send_keys(Keys.CONTROL, "t").perform(),但是,这没有任何作用。

我尝试了以下方法来测试 Keys.CONTROL 是否正常工作:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "v").perform()

这确实会将我在剪贴板中复制的所有内容传递到默认聚焦的 Google 搜索框。

这是我想使用的,但它不起作用:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()

浏览器似乎没有任何反应,没有打开新标签页,没有对话框,没有通知。有谁知道这是为什么吗?

最佳答案

试试这个 java Script Executor,它应该可以工作。

link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))

已编辑 带有窗口句柄的代码。

driver=webdriver.Chrome()
driver.get("https://www.google.com")
window_before = driver.window_handles[0]

link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)

driver.find_element_by_name("q").send_keys("test")

enter image description here

关于python - 使用 ActionChains 在 Selenium Webdriver 中按 ctrl+t 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55161221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com