gpt4 book ai didi

java - Selenium - 单击链接打开一个新选项卡

转载 作者:搜寻专家 更新时间:2023-10-31 19:44:39 25 4
gpt4 key购买 nike

我看过很多关于如何在新标签页中打开链接的帖子,但是当您有一个创建新标签页的链接并且需要验证标题时怎么办?我需要做的就是单击链接 --> 验证新选项卡的标题是否正确 --> 关闭选项卡并在原始选项卡上继续。在此先感谢您的帮助!

最佳答案

//Get Current Page 
String currentPageHandle = driver.getWindowHandle();
linkToClick.click();

//Add Logic to Wait till Page Load

// Get all Open Tabs
ArrayList<String> tabHandles = new ArrayList<String>(driver.getWindowHandles());

String pageTitle = "ThePageTitleIhaveToCheckFor";
boolean myNewTabFound = false;

for(String eachHandle : tabHandles)
{
driver.switchTo().window(eachHandle);
// Check Your Page Title
if(driver.getTitle().equalsIgnoreCase(pageTitle))
{
// Report ur new tab is found with appropriate title

//Close the current tab
driver.close(); // Note driver.quit() will close all tabs

//Swithc focus to Old tab
driver.switchTo().window(currentPageHandle);
myNewTabFound = true;
}
}

if(myNewTabFound)
{
// Report page not opened as expected
}

关于java - Selenium - 单击链接打开一个新选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067381/

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