How to open a new tab using Selenium WebDriver?

后端 未结 29 2485
野的像风
野的像风 2020-11-22 04:40

How to open a new tab in the existing Firefox browser using Selenium WebDriver (a.k.a. Selenium 2)?

相关标签:
29条回答
  • 2020-11-22 05:24

    I had trouble opening a new tab in chrome for a while. Even driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); Didn't work for me.

    I found out that it's not enough that selenium has focus on driver, Windows also has to have the window in the front.

    My solution was to invoke an alert on chrome that would bring the window to front and then execute the command. sample code:

    ((JavascriptExecutor)driver).executeScript("alert('Test')");
    driver.switchTo().alert().accept();
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
    
    0 讨论(0)
  • 2020-11-22 05:25
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");// open in new tab
    driver.get("ur link");
    
    0 讨论(0)
  • 2020-11-22 05:26

    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));
    
    0 讨论(0)
  • 2020-11-22 05:26

    To open new window in Chrome Driver.

    //The script that will will open a new blank window
    //If you want to open a link new tab, replace 'about:blank' with a link
    String a = "window.open('about:blank','_blank');";
    ((JavascriptExecutor)driver).executeScript(a);
    

    For switching between tabs, Read here

    0 讨论(0)
  • 2020-11-22 05:27

    check this complete example to understand how to open multiple tabs and switch between the tabs and at the end close all tabs

    public class Tabs {
    
     WebDriver driver; 
    
     Robot rb;
    
    
     @BeforeTest
     public void setup() throws Exception {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
      WebDriver driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("http://qaautomated.com");
     }
    
     @Test
     public void openTab() {
      //Open tab 2 using CTRL + t keys.
      driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
    
    
     //Open URL In 2nd tab.
      driver.get("http://www.qaautomated.com/p/contact.html");
    
      //Call switchToTab() method to switch to 1st tab
      switchToTab(); 
    
    
      //Call switchToTab() method to switch to 2nd tab.
      switchToTab();
    
    
     } 
    
     public void switchToTab() {
      //Switching between tabs using CTRL + tab keys.
      driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
      //Switch to current selected tab's content.
      driver.switchTo().defaultContent();  
     } 
    
    
    
    @AfterTest
     public void closeTabs() throws AWTException {
      //Used Robot class to perform ALT + SPACE + 'c' keypress event.
      rb =new Robot();
      rb.keyPress(KeyEvent.VK_ALT);
      rb.keyPress(KeyEvent.VK_SPACE);
      rb.keyPress(KeyEvent.VK_C);
     } }
    

    This example is given by this web page

    0 讨论(0)
  • 2020-11-22 05:27

    Question : How to open a new tab using Selenium WebDriver with Java?

    Answer : After click on any link open new tab.

    If we want to handle newly open tab then we have need to handle tab using .switchTo().window() command.

    Switch to particular tab then perform operation and switch back to into parent tab.

    package test;
    
    import java.util.ArrayList;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Tab_Handle {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "geckodriver_path");
    
            WebDriver driver = new FirefoxDriver();
    
            driver.get("http://www.google.com");
    
            // Store all currently open tabs in Available_tabs
            ArrayList<String> Available_tabs = new ArrayList<String>(driver.getWindowHandles());
    
            // Click on link to open in new tab
            driver.findElement(By.id("Url_Link")).click();
    
            // Switch newly open Tab
            driver.switchTo().window(Available_tabs.get(1));
    
            // Perform some operation on Newly open tab
            // Close newly open tab after performing some operations.
            driver.close();
    
            // Switch to old(Parent) tab.
            driver.switchTo().window(Available_tabs.get(0));
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题