webdriver java a标签_如何使用Selenium WebDriver打开新标签页并启动链接?

该博客介绍了如何使用Selenium WebDriver在Java中通过模拟键盘快捷键(Ctrl+T)来打开新的浏览器标签页,并在新标签页中加载指定URL。示例代码展示了如何在不同标签页间切换进行自动化测试操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import java.awt.Robot;

import java.awt.event.KeyEvent;

import java.awt.AWTException;

public class Tabs {

WebDriver driver;

Robot rb;

@BeforeTest

public void setup() throws Exception {

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

driver.get("http://qaautomated.com");

}

@Test

public void openTab() {

//Open tab 2 using CTRL + t keys.

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

//Open URL In 2nd tab.

driver.get("http://www.qaautomated.com/p/contact.html");

//Call switchToTab() method to switch to 1st tab

switchToTab();

//perform required actions on tab 1.

driver.findElement(By.xpath("//input[@id='6']")).click();

driver.findElement(By.xpath("//input[@id='plus']"));

driver.findElement(By.xpath("//input[@id='3']"));

driver.findElement(By.xpath("//input[@id='equals']"));

//Call switchToTab() method to switch to 2nd tab.

switchToTab();

//Call switchToTab() method to switch to 1st tab

switchToTab();

}

public void switchToTab() {

//Switching between tabs using CTRL + tab keys.

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

//Switch to current selected tab's content.

driver.switchTo().defaultContent();

}

@AfterTest

public void closeTabs() throws AWTException {

//Used Robot class to perform ALT + SPACE + 'c' keypress event.

rb =new Robot();

rb.keyPress(KeyEvent.VK_ALT);

rb.keyPress(KeyEvent.VK_SPACE);

rb.keyPress(KeyEvent.VK_C);

} }此示例取自THIS BLOG POST

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值