How to open a new tab using Selenium WebDriver?

后端 未结 29 3028
野的像风
野的像风 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: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

提交回复
热议问题