I am using Selenium WebDriver. Every link is opened in a new browser window. It is not convenient for me. How can I change it so that it opens just in new tab?
Selenium has no ability to switch tabs at the moment. Because of this we force the browser to open links in new windows but since we are able to switch windows we force the browser to take the approach. This may be fixed in a later version
Selenium has the ability to switch over tabs now-a-days. The below code1: will work for firefox, code2: for IE and chrome by using Robot class we can do and the control doesnt move automatically to current tab so we need to switch to the current tab by using window handles method. The given below code will work well When we are running individual script but when running as a suite you may feel the pain in performing key board events. In order to avoid that we got to go with other possibility by using user defined javascript method by using javascript executor in selenium-Java.
We can switch between windows and tabs by identifying its name allocated for each and every windows which we open and the name will be in alphanumeric character.
***Code 1***
//First tab(default tab)
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
//second tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.navigate().to("https://yahoo.com");
//third tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.navigate().to("http://www.google.com");
//move to very first tab.
driver.findElement(By.cssSelector("body"))
.sendKeys(Keys.CONTROL + "\t");
// To close the current tab.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
**code 2**
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
Set<String> handles = driver.getWindowHandles();
List<String> handlesList = new ArrayList<String>(handles);
String newTab = handlesList.get(handlesList.size() - 1);
// switch to new tab
driver.switchTo().window(newTab);
driver.get("http://www.yahoo.com");
Selenium has no ability to switch tabs at the moment. Because of this we force the browser to open links in new windows but since we are able to switch windows we force the browser to take the approach. This may be fixed in a later version
If you don't care about switching between tabs, which is a high probability, just use the following options in firefox:
FirefoxOptions options = new FirefoxOptions().addPreference("browser.link.open_newwindow", 3);
WebDriver driver1 = new new FirefoxDriver(options);
As the first link below explains, Selenium wanted to give the ability to users to switch between tabs. The workaround to achieve this was to instruct the browser to open tabs as windows. This however has some undesirable side-efects like:
- you have to manually switch to the tab that a link just opened (and use tricks to find which is this tab).
- you have to manually switch back to the original tab if you close the current one.
The code above reverts this option for firefox.
Links:
© 2022 - 2024 — McMap. All rights reserved.