How to open a link in a 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); 

A new tab opens, but the link to the URL does not open.

+9
source share
8 answers

I checked the code below and it works great for me. I found the 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"); 
+11
source

this code below works for me in Selenium 3 and chrome version 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"); 
+20
source

I tried other methods, but none of them worked, there were no errors either, but when I 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"); 
+6
source

First open an empty new tab with Ctrl + t , and then use .get() to get the URL you want, your code should look something like this:

 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 to the current view in a new tab, you can use the code you wrote above. Instead of By.linkText() make sure you select the By element to select the web element.

+1
source

I used the code below to open a new tab in a browser using C # selenium ..

 IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("window.open();"); 
+1
source

Selenium can only be automated on browser web elements. Opening a new tab is an operation performed in a web browser, which is a stand-alone application. You can use the Robot class from the java.util package for this. *, Which can perform operations using the keyboard, regardless of the type of application. So here is the code for your work. Please note that you cannot automate standalone applications using the Robot class, but you can perform operations with the keyboard or mouse

 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 go to a 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); 
0
source

If you can get the link element, you can use this. It will also take you to an open tab.

 WebElement link= driver.findElement(By.tagname("a")); String keyString = Keys.CONTROL+Keys.SHIFT.toString()+Keys.ENTER.toString()); link.sendKeys(keyString); 
0
source

You can open multiple browsers or a window using the following 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"); 
-2
source

Source: https://habr.com/ru/post/1240761/


All Articles