gpt4 book ai didi

google-chrome - 如何使用 webdriver 在 chrome 中打开新窗口而不是新选项卡?

转载 作者:行者123 更新时间:2023-12-03 03:03:51 28 4
gpt4 key购买 nike

为了自动化我的测试应用程序,我需要在新窗口而不是选项卡中打开一些链接。请记住,我没有明确在新选项卡中打开链接,而是我的网络应用程序在单击链接后自动将用户引导到新选项卡中。

我为什么要这样做?

因为在 Chrome 浏览器上运行测试会关闭主选项卡并保持打开新打开的选项卡。最终测试失败。因此最终目的是打开新窗口而不是选项卡并使用 driver.getWindowHandles() 正确处理它。

到目前为止我做了什么?

我试图在 Chrome 中找到某种功能设置或配置文件,它会自动在新窗口中打开本应在选项卡中打开的链接。但没有找到任何令人信服的解决方案,大多数建议是 CTRL+CLICK在链接上。

最佳答案

我不是网页设计大师,但我可以建议以下场景:

// Get required page
// Excecute below JavaScript with JavaScriptExecutor
var reference = document.querySelector('a#someID').getAttribute('href'); // You can use your specific CSS Selector instead of "a#someID"
document.querySelector('a#someID').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#someID').removeAttribute('href')
// Find target link
// Click on it

此代码应允许您对目标 Web 元素的 HTML 源代码进行更改,以强制其在新浏览器窗口中打开。

注意,使用此代码元素在页面上的外观将发生变化,直到页面刷新

附注您没有提到您的编程语言,因此没有完整的实现...但是,这是 Python 实现示例:

from selenium import webdriver as web

dr = web.Chrome()
dr.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')

dr.execute_script("""
var reference = document.querySelector('a#ftrTerms').getAttribute('href');
document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#ftrTerms').removeAttribute('href')
""")
link = dr.find_element_by_id('ftrTerms')
link.click()

关于google-chrome - 如何使用 webdriver 在 chrome 中打开新窗口而不是新选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41480851/

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