Java 如何使用 Selenium WebDriver 在新选项卡(chrome)中打开链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34829329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a link in new tab (chrome) using Selenium WebDriver?
提问by nlogn
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);
New tab is opening but URL link is not opening.
新选项卡正在打开,但 URL 链接未打开。
采纳答案by Abdul Hameed
I checked with below code and it works fine for me. I found answer from here.
我检查了下面的代码,它对我来说很好用。我从这里找到了答案。
driver = new ChromeDriver();
driver.manage().window().maximize();
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");
回答by JRodDynamite
First open empty new Tab by using the keys Ctrl+ tand then use .get()
to fetch the URL you want. Your code should look something like this -
首先使用键Ctrl+打开空的新选项卡t,然后使用.get()
获取所需的 URL。你的代码应该是这样的 -
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.get("www.facebook.com");
If you want to open a link on the current view in a new tab then the code you've written above can be used. Instead of By.linkText()
make sure you use the appropriate By
selector class to select the web element.
如果要在新选项卡中打开当前视图上的链接,则可以使用上面编写的代码。而不是By.linkText()
确保使用适当的By
选择器类来选择 Web 元素。
回答by nur
this below code works for me in Selenium 3 and chrome version 58.
下面的代码适用于 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");
回答by satheesh kumar P
You can open multiple browser or a window by using below code:
您可以使用以下代码打开多个浏览器或窗口:
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");
回答by Gaurav Thantry
Selenium can only automate on the WebElements of the browser. Opening a new tab is an operation performed on the webBrowser which is a stand alone application. For doing this you can make use of the Robot class from the java.util.* package which can perform operations using the keyboard regardless of what type of application it is. So here's the code for your operation. Note that you cannot automate stand alone applications using the Robot class but you can perform keyboard or mouse operations
Selenium 只能在浏览器的 WebElements 上自动化。打开新标签是在 webBrowser 上执行的操作,它是一个独立的应用程序。为此,您可以使用 java.util.* 包中的 Robot 类,该类可以使用键盘执行操作,而不管它是什么类型的应用程序。所以这是您操作的代码。 请注意,您不能使用 Robot 类自动化独立应用程序,但您可以执行键盘或鼠标操作
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);
After this step you will need a window iterator to switch to the new tab:
在这一步之后,您将需要一个窗口迭代器来切换到新选项卡:
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);
回答by mani kandan
I had used the below code to open a new tab in the browser using C# selenium..
我使用以下代码在使用 C# selenium 的浏览器中打开了一个新选项卡。
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
回答by Ziaullah
I have tried other techniques, but none of them worked, also no error produced, but when I have used the code below, it worked for me.
我尝试过其他技术,但都没有奏效,也没有产生错误,但是当我使用下面的代码时,它对我有用。
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
回答by user2117229
If you can get the link element you can use this. It will also take you to the tab that you have opened.
如果您可以获得链接元素,则可以使用它。它还将带您到您打开的选项卡。
WebElement link= driver.findElement(By.tagname("a"));
String keyString = Keys.CONTROL+Keys.SHIFT.toString()+Keys.ENTER.toString());
link.sendKeys(keyString);
回答by Mike ASP
Selenium 4 is already included this feature now, you can directly
open new Tab or new Window with any URL.
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");
download Maven dependencies, this is what I downloaded -
<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>
you can refer: https://codoid.com/selenium-4-0-command-to-open-new-window-tab/
watch video : https://www.youtube.com/watch?v=7SpCMkUKq-Y&t=8s
google out for - WebDriverManager selenium 4
回答by Junior Gr?o
I am trying to do a robot to my little son and just play a Youtube video and than show a robot dancing.
我正在尝试为我的小儿子做一个机器人,然后播放 Youtube 视频,而不是展示机器人跳舞。
For some reason, commands like CONTROL + T explained above was not working for me and maybe it is not the correct answer but I solved my problem using custom Javascript script like this:
出于某种原因,像上面解释的 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);
}