简体   繁体   中英

How to open multiple tabs and switch between through Selenium and Webdriver?

I googled for this code, but not getting proper code. I have a scenario where our application having 5 modules, i want to open it on each tab because i need to switch between them multiple times plz help

this is not working:

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

Using getWindowHandles you can switch url multiple time.Here first i switch url and identify the tab,Everytab should a unique url and we can identify it by url.

   for (String window : driver.getWindowHandles()) {
    driver.switchTo().window(window);
    if (driver.getCurrentUrl().contains("google.com")) {    
     //Your operation     
    }       
    if (driver.getCurrentUrl().contains("yahoo.com")) {
    }
    //Your operation
    }

For example you open google and yahoo in two tab then you can switch tab and identify the tab and do any operation there.

Hope it will help you

Here is the sample example to open multiple tabs and switch between them through Selenium Webdriver:

  • Code Block:

     import java.util.Set; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class WINDOW_HANDLE_ITERATE_Firefox { public static void main(String[] args) throws Exception { System.setProperty("webdriver.gecko.driver", "C:\\\\Utility\\\\BrowserDrivers\\\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); String parent_window = driver.getWindowHandle(); System.out.println("Parent Window Handle is: "+driver.getWindowHandle()); System.out.println("Page Title is: "+driver.getTitle()); ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');"); new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2)); Set<String> allWindows_1 = driver.getWindowHandles(); System.out.println("Total Windows: "+allWindows_1.size()); for(String hand1:allWindows_1) if(!parent_window.equals(hand1)) { driver.switchTo().window(hand1); new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Facebook")); String first_child_window = driver.getWindowHandle(); System.out.println("First Child Window Handle is: "+first_child_window); System.out.println("First Child Window Page Title is: "+driver.getTitle()); driver.close(); } driver.switchTo().window(parent_window); System.out.println("Current Window Handle is : "+driver.getWindowHandle()+ " which is same as "+parent_window +", which is the parent window handle" ); driver.quit(); } } 
  • Console Output:

     INFO: Detected dialect: W3C Parent Window Handle is: 6442450945 Page Title is: Google Total Windows: 2 First Child Window Handle is: 6442450949 First Child Window Page Title is: Facebook – log in or sign up Current Window Handle is : 6442450945 which is same as 6442450945, which is the parent window handle 

You may try this,

To open new tab using JavascriptExecutor,

((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

Will control on tab as according to index:

driver.switchTo().window(tabs.get(1));

Driver control on main tab:

driver.switchTo().window(tabs.get(0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM