如何使用 Selenium WebDriver 在新选项卡 (chrome) 中打开 link?
How to open a link in new tab (chrome) using Selenium WebDriver?
System.setProperty("webdriver.chrome.driver", "D:\softwares\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
新标签页正在打开,但 URL link 未打开。
首先使用 Ctrl + t 打开空的新标签页,然后使用 .get()
获取 URL 你想要的。您的代码应如下所示 -
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.get("www.facebook.com");
如果您想在新选项卡中的当前视图上打开 link,则可以使用您在上面编写的代码。请确保使用适当的 By
select 或 class 代替 By.linkText()
到 select 网络元素。
我检查了以下代码,它对我来说工作正常。我从 here.
找到了答案
driver = new ChromeDriver();
driver.manage().window().maximize();
String baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.news.google.com");
下面的代码适用于 Selenium 3 和 chrome 版本 58。
WebDriver driver = new ChromeDriver();
driver.get("http://yahoo.com");
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
您可以使用以下代码打开多个浏览器或 window:
WebDriver driver = new ChromeDriver();
driver.get("http://yahoo.com");
WebDriver driver1 = new ChromeDriver();
driver1.get("google.com");
WebDriver driver2 = new InternetExplorerDriver();
driver2.get("google.com/gmap");
Selenium 只能在浏览器的 WebElements 上实现自动化。打开新选项卡是在 webBrowser 上执行的操作,它是一个独立的应用程序。为此,您可以使用 java.util.* 包中的机器人 class,无论它是什么类型的应用程序,它都可以使用键盘执行操作。
所以这是您的操作代码。
请注意,您无法使用机器人自动执行独立应用程序 class,但您可以执行键盘或鼠标操作
System.setProperty("webdriver.chrome.driver","softwares\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.google.com");
Robot rob = new Robot();
rob.keyPress(keyEvent.VK_CONTROL);
rob.keyPress(keyEvent.VK_T);
rob.keyRelease(keyEvent.VK_CONTROL);
rob.keyRelease(keyEvent.VK_T);
完成此步骤后,您将需要一个 window 迭代器来切换到新选项卡:
Set <String> ids = driver.getWindowHandles();
Iterator <String> it = ids.iterator();
String currentWindow = it.next();
String newWindow = it.next();
driver.switchTo().window(newWindow);
driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
我使用下面的代码使用 C# selenium 在浏览器中打开一个新选项卡..
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
我尝试过其他技术,但是 none 有效,也没有产生错误,但是当我使用下面的代码时,它对我有用。
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
如果您可以获得 link 元素,您就可以使用它。它还会将您带到您打开的选项卡。
WebElement link= driver.findElement(By.tagname("a"));
String keyString = Keys.CONTROL+Keys.SHIFT.toString()+Keys.ENTER.toString());
link.sendKeys(keyString);
Selenium 4 现在已经包含此功能,您可以直接打开新选项卡或使用任何 URL.
打开新 Window
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
driver.get("www.Url1.com");
// below code will open Tab for you as well as switch the control to new Tab
driver.switchTo().newWindow(WindowType.TAB);
// below code will navigate you to your desirable Url
driver.get("www.Url2.com");
下载Maven依赖,这是我下载的-
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
您可以参考:https://codoid.com/selenium-4-0-command-to-open-new-window-tab/
观看视频:https://www.youtube.com/watch?v=7SpCMkUKq-Y&t=8s
google 退出 - WebDriverManager selenium 4
我想给我的小儿子做一个机器人,只是播放 Youtube 视频,而不是展示机器人跳舞。
出于某种原因,上面解释的像 CONTROL + T 这样的命令对我不起作用,也许这不是正确的答案,但我使用自定义 Javascript 脚本解决了我的问题,如下所示:
using (var driver = new ChromeDriver())
{
var link1 = "https://www.youtube.com/watch?v=0GIgk4yuHOQ";
//open a music
driver.Navigate().GoToUrl(link1);
var link2 = "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/fbe53d6d-c13f-4eec-9bcf-62f19cfab15a/d4m0h4v-9442b1f2-6a49-4818-8f51-5ebe216f043c.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwic3ViIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsImF1ZCI6WyJ1cm46c2VydmljZTpmaWxlLmRvd25sb2FkIl0sIm9iaiI6W1t7InBhdGgiOiIvZi9mYmU1M2Q2ZC1jMTNmLTRlZWMtOWJjZi02MmYxOWNmYWIxNWEvZDRtMGg0di05NDQyYjFmMi02YTQ5LTQ4MTgtOGY1MS01ZWJlMjE2ZjA0M2MuZ2lmIn1dXX0.BTTlingNpBqH5O9dNVienFsArNqkfUc7KXnIgHumrBQ";
//Dance robot, dance
driver.ExecuteScript($"window.open('{link2}', '_blank');");
Thread.Sleep(20000);
}
单击预期从新选项卡打开的 link 使用此
WebDriver driver = new ChromeDriver();
driver.get("https://www.yourSite.com");
WebElement link=driver.findElement(By.xpath("path_to_link"));
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(element)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
使用 .
在新标签页中打开 link 有多种方法
用例 A:打开相邻的空白选项卡并遍历迭代器
代码块:
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NewTab_blank_iterator {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\WebDrivers\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://mail.google.com/");
String firstWindowHandle = driver.getWindowHandle();
System.out.println("First Window Handle is: "+firstWindowHandle);
// Opening an adjacent blank tab
((JavascriptExecutor)driver).executeScript("window.open('','_blank');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allWindowHandles = driver.getWindowHandles();
// Using iterator
Iterator<String> itr = allWindowHandles.iterator();
while(itr.hasNext()) {
String nextWindow = itr.next();
if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
driver.switchTo().window(nextWindow);
System.out.println("New Tab Window Handle is: "+nextWindow);
}
}
}
}
控制台输出:
First Window Handle is: CDwindow-0D89767363ED691767000F01E6712D0B
New Tab Window Handle is: CDwindow-7232D2058514ED22344F129D30A0CCE7
浏览器快照:
用例 B:使用 url 打开相邻选项卡并遍历迭代器
代码块:
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NewTab_url_forLoop {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\WebDrivers\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
String url1 = "https://mail.google.com/";
String url2 = "https://www.facebook.com/";
driver.get(url1);
String firstWindowHandle = driver.getWindowHandle();
System.out.println("First Window Handle is: "+firstWindowHandle);
// Opening Facebook in the adjacent TAB
((JavascriptExecutor)driver).executeScript("window.open('" + url2 +"');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allWindowHandles = driver.getWindowHandles();
// Using iterator
Iterator<String> itr = allWindowHandles.iterator();
while(itr.hasNext()) {
String nextWindow = itr.next();
if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
driver.switchTo().window(nextWindow);
System.out.println("New Tab Window Handle is: "+nextWindow);
}
}
}
}
控制台输出:
First Window Handle is: CDwindow-01F5622275A2EA2C1ABE2F0CDEB3D09B
New Tab Window Handle is: CDwindow-9E3349B91FB2FA4D5B7D4A90D0E87BD3
浏览器快照:
在选项卡之间切换操作 class 并不总是适用于所有浏览器和所有网络驱动程序。最好的方法是使用机器人 class。试试这个代码。
String website = "https://www.google.com";
String website1 = "https://www.msn.com/en-in/";
String controlpath = "C:\Libraries\msedgedriver.exe";
System.setProperty("webdriver.edge.driver", controlpath);
driver = new EdgeDriver();
driver.manage().window().maximize(); // Maximize browser
driver.get(website);
System.out.println("Google page");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
//System.out.println("Handle info"+ driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Launch URL in the new tab
driver.get(website1);
System.out.println("msn page");
Thread.sleep(5000);
driver.switchTo().window(tabs.get(0));
Thread.sleep(5000);
楼主问的是如何在新标签页打开link。这就是我在 C# 中的做法。
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://microsoft.com");
IWebElement eventlink = driver.FindElement(By.Id("uhfLogo"));
Actions action = new Actions(driver);
action.KeyDown(Keys.Control).MoveToElement(eventlink).Click().Perform();
这实际上是在所选元素上执行 Control+单击,以便在新选项卡中打开 link。
System.setProperty("webdriver.chrome.driver", "D:\softwares\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
新标签页正在打开,但 URL link 未打开。
首先使用 Ctrl + t 打开空的新标签页,然后使用 .get()
获取 URL 你想要的。您的代码应如下所示 -
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.get("www.facebook.com");
如果您想在新选项卡中的当前视图上打开 link,则可以使用您在上面编写的代码。请确保使用适当的 By
select 或 class 代替 By.linkText()
到 select 网络元素。
我检查了以下代码,它对我来说工作正常。我从 here.
找到了答案 driver = new ChromeDriver();
driver.manage().window().maximize();
String baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.news.google.com");
下面的代码适用于 Selenium 3 和 chrome 版本 58。
WebDriver driver = new ChromeDriver();
driver.get("http://yahoo.com");
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
您可以使用以下代码打开多个浏览器或 window:
WebDriver driver = new ChromeDriver();
driver.get("http://yahoo.com");
WebDriver driver1 = new ChromeDriver();
driver1.get("google.com");
WebDriver driver2 = new InternetExplorerDriver();
driver2.get("google.com/gmap");
Selenium 只能在浏览器的 WebElements 上实现自动化。打开新选项卡是在 webBrowser 上执行的操作,它是一个独立的应用程序。为此,您可以使用 java.util.* 包中的机器人 class,无论它是什么类型的应用程序,它都可以使用键盘执行操作。 所以这是您的操作代码。 请注意,您无法使用机器人自动执行独立应用程序 class,但您可以执行键盘或鼠标操作
System.setProperty("webdriver.chrome.driver","softwares\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.google.com");
Robot rob = new Robot();
rob.keyPress(keyEvent.VK_CONTROL);
rob.keyPress(keyEvent.VK_T);
rob.keyRelease(keyEvent.VK_CONTROL);
rob.keyRelease(keyEvent.VK_T);
完成此步骤后,您将需要一个 window 迭代器来切换到新选项卡:
Set <String> ids = driver.getWindowHandles();
Iterator <String> it = ids.iterator();
String currentWindow = it.next();
String newWindow = it.next();
driver.switchTo().window(newWindow);
driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
我使用下面的代码使用 C# selenium 在浏览器中打开一个新选项卡..
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
我尝试过其他技术,但是 none 有效,也没有产生错误,但是当我使用下面的代码时,它对我有用。
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
如果您可以获得 link 元素,您就可以使用它。它还会将您带到您打开的选项卡。
WebElement link= driver.findElement(By.tagname("a"));
String keyString = Keys.CONTROL+Keys.SHIFT.toString()+Keys.ENTER.toString());
link.sendKeys(keyString);
Selenium 4 现在已经包含此功能,您可以直接打开新选项卡或使用任何 URL.
打开新 WindowWebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
driver.get("www.Url1.com");
// below code will open Tab for you as well as switch the control to new Tab
driver.switchTo().newWindow(WindowType.TAB);
// below code will navigate you to your desirable Url
driver.get("www.Url2.com");
下载Maven依赖,这是我下载的-
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
您可以参考:https://codoid.com/selenium-4-0-command-to-open-new-window-tab/
观看视频:https://www.youtube.com/watch?v=7SpCMkUKq-Y&t=8s
google 退出 - WebDriverManager selenium 4
我想给我的小儿子做一个机器人,只是播放 Youtube 视频,而不是展示机器人跳舞。
出于某种原因,上面解释的像 CONTROL + T 这样的命令对我不起作用,也许这不是正确的答案,但我使用自定义 Javascript 脚本解决了我的问题,如下所示:
using (var driver = new ChromeDriver())
{
var link1 = "https://www.youtube.com/watch?v=0GIgk4yuHOQ";
//open a music
driver.Navigate().GoToUrl(link1);
var link2 = "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/fbe53d6d-c13f-4eec-9bcf-62f19cfab15a/d4m0h4v-9442b1f2-6a49-4818-8f51-5ebe216f043c.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwic3ViIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsImF1ZCI6WyJ1cm46c2VydmljZTpmaWxlLmRvd25sb2FkIl0sIm9iaiI6W1t7InBhdGgiOiIvZi9mYmU1M2Q2ZC1jMTNmLTRlZWMtOWJjZi02MmYxOWNmYWIxNWEvZDRtMGg0di05NDQyYjFmMi02YTQ5LTQ4MTgtOGY1MS01ZWJlMjE2ZjA0M2MuZ2lmIn1dXX0.BTTlingNpBqH5O9dNVienFsArNqkfUc7KXnIgHumrBQ";
//Dance robot, dance
driver.ExecuteScript($"window.open('{link2}', '_blank');");
Thread.Sleep(20000);
}
单击预期从新选项卡打开的 link 使用此
WebDriver driver = new ChromeDriver();
driver.get("https://www.yourSite.com");
WebElement link=driver.findElement(By.xpath("path_to_link"));
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(element)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
使用
用例 A:打开相邻的空白选项卡并遍历迭代器
代码块:
import java.util.Iterator; import java.util.Set; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class NewTab_blank_iterator { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\WebDrivers\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); WebDriver driver = new ChromeDriver(options); driver.get("https://mail.google.com/"); String firstWindowHandle = driver.getWindowHandle(); System.out.println("First Window Handle is: "+firstWindowHandle); // Opening an adjacent blank tab ((JavascriptExecutor)driver).executeScript("window.open('','_blank');"); new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2)); Set<String> allWindowHandles = driver.getWindowHandles(); // Using iterator Iterator<String> itr = allWindowHandles.iterator(); while(itr.hasNext()) { String nextWindow = itr.next(); if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) { driver.switchTo().window(nextWindow); System.out.println("New Tab Window Handle is: "+nextWindow); } } } }
控制台输出:
First Window Handle is: CDwindow-0D89767363ED691767000F01E6712D0B New Tab Window Handle is: CDwindow-7232D2058514ED22344F129D30A0CCE7
浏览器快照:
用例 B:使用 url 打开相邻选项卡并遍历迭代器
代码块:
import java.util.Iterator; import java.util.Set; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class NewTab_url_forLoop { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\WebDrivers\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); WebDriver driver = new ChromeDriver(options); String url1 = "https://mail.google.com/"; String url2 = "https://www.facebook.com/"; driver.get(url1); String firstWindowHandle = driver.getWindowHandle(); System.out.println("First Window Handle is: "+firstWindowHandle); // Opening Facebook in the adjacent TAB ((JavascriptExecutor)driver).executeScript("window.open('" + url2 +"');"); new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2)); Set<String> allWindowHandles = driver.getWindowHandles(); // Using iterator Iterator<String> itr = allWindowHandles.iterator(); while(itr.hasNext()) { String nextWindow = itr.next(); if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) { driver.switchTo().window(nextWindow); System.out.println("New Tab Window Handle is: "+nextWindow); } } } }
控制台输出:
First Window Handle is: CDwindow-01F5622275A2EA2C1ABE2F0CDEB3D09B New Tab Window Handle is: CDwindow-9E3349B91FB2FA4D5B7D4A90D0E87BD3
浏览器快照:
在选项卡之间切换操作 class 并不总是适用于所有浏览器和所有网络驱动程序。最好的方法是使用机器人 class。试试这个代码。
String website = "https://www.google.com";
String website1 = "https://www.msn.com/en-in/";
String controlpath = "C:\Libraries\msedgedriver.exe";
System.setProperty("webdriver.edge.driver", controlpath);
driver = new EdgeDriver();
driver.manage().window().maximize(); // Maximize browser
driver.get(website);
System.out.println("Google page");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
//System.out.println("Handle info"+ driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Launch URL in the new tab
driver.get(website1);
System.out.println("msn page");
Thread.sleep(5000);
driver.switchTo().window(tabs.get(0));
Thread.sleep(5000);
楼主问的是如何在新标签页打开link。这就是我在 C# 中的做法。
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://microsoft.com");
IWebElement eventlink = driver.FindElement(By.Id("uhfLogo"));
Actions action = new Actions(driver);
action.KeyDown(Keys.Control).MoveToElement(eventlink).Click().Perform();
这实际上是在所选元素上执行 Control+单击,以便在新选项卡中打开 link。