webdriver java a标签_用Java使用Selenium WebDriver切换选项卡

这是一个简单的解决方案,用于打开一个新选项卡,将焦点更改为它,关闭该选项卡并将焦点返回到旧/原始选项卡:@Testpublic void testTabs() {

driver.get("https://business.twitter.com/start-advertising");

assertStartAdvertising();

// considering that there is only one tab opened in that point.

String oldTab = driver.getWindowHandle();

driver.findElement(By.linkText("Twitter Advertising Blog")).click();

ArrayList newTab = new ArrayList(driver.getWindowHandles());

newTab.remove(oldTab);

// change focus to new tab

driver.switchTo().window(newTab.get(0));

assertAdvertisingBlog();

// Do what you want here, you are in the new tab

driver.close();

// change focus back to old tab

driver.switchTo().window(oldTab);

assertStartAdvertising();

// Do what you want here, you are in the old tab}private void assertStartAdvertising() {

assertEquals("Start Advertising | Twitter for Business", driver.getTitle());}private void assertAdvertisingBlog() {

assertEquals("Twitter Advertising", driver.getTitle());}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值