How to open a link in new tab (chrome) using Selenium WebDriver?

后端 未结 14 2403
余生分开走
余生分开走 2020-11-30 03:37
System.setProperty(\"webdriver.chrome.driver\", \"D:\\\\softwares\\\\chromedriver_win32\\\\chromedriver.exe\");

WebDriver driver = new ChromeDriver();
driver.manage         


        
相关标签:
14条回答
  • 2020-11-30 04:08

    Selenium can only automate on the WebElements of the browser. Opening a new tab is an operation performed on the webBrowser which is a stand alone application. For doing this you can make use of the Robot class from the java.util.* package which can perform operations using the keyboard regardless of what type of application it is. So here's the code for your operation. Note that you cannot automate stand alone applications using the Robot class but you can perform keyboard or mouse operations

    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 switch to the 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 讨论(0)
  • 2020-11-30 04:09
    Selenium 4 is already included this feature now, you can directly 
    open new Tab or new Window with any URL. 
    
    WebDriverManager.chromedriver().setup();
    
    driver = new ChromeDriver(options);
    
    driver.get("www.Url1.com");     
    //  below code will open Tab for you as well as switch the control to new Tab
    driver.switchTo().newWindow(WindowType.TAB);
    
    // below code will navigate you to your desirable Url 
    driver.get("www.Url2.com");
    
    download Maven dependencies, this is what I downloaded - 
    
            <dependency>
                <groupId>io.github.bonigarcia</groupId>
                <artifactId>webdrivermanager</artifactId>
                <version>3.7.1</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.6</version>
            </dependency> 
    
        you can refer: https://codoid.com/selenium-4-0-command-to-open-new-window-tab/
    
        watch video : https://www.youtube.com/watch?v=7SpCMkUKq-Y&t=8s
    
    google out for - WebDriverManager selenium 4
    
    0 讨论(0)
  • 2020-11-30 04:11

    There are multiple ways to open a link in new tab in using Selenium WebDriver.


    Usecase A: Opening an adjacent blank tab and iterating through an iterator

    • Code Block:

      import java.util.Iterator;
      import java.util.Set;
      
      import org.openqa.selenium.JavascriptExecutor;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.chrome.ChromeDriver;
      import org.openqa.selenium.chrome.ChromeOptions;
      import org.openqa.selenium.support.ui.ExpectedConditions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class NewTab_blank_iterator {
      
          public static void main(String[] args) {
      
              System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
              ChromeOptions options = new ChromeOptions();
              options.addArguments("start-maximized");
              WebDriver driver =  new ChromeDriver(options); 
              driver.get("https://mail.google.com/");
              String firstWindowHandle = driver.getWindowHandle();
              System.out.println("First Window Handle is: "+firstWindowHandle);
              // Opening an adjacent blank tab
              ((JavascriptExecutor)driver).executeScript("window.open('','_blank');");
              new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
              Set<String> allWindowHandles = driver.getWindowHandles();
              // Using iterator
              Iterator<String> itr = allWindowHandles.iterator();
              while(itr.hasNext()) {
                  String nextWindow = itr.next();
                  if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
                      driver.switchTo().window(nextWindow);
                      System.out.println("New Tab Window Handle is: "+nextWindow);
                  }
              }
          }
      }
      
    • Console Output:

      First Window Handle is: CDwindow-0D89767363ED691767000F01E6712D0B
      New Tab Window Handle is: CDwindow-7232D2058514ED22344F129D30A0CCE7
      
    • Browser Snapshot:


    Usecase B: Opening an adjacent tab with an url and and iterating through an iterator

    • Code Block:

      import java.util.Iterator;
      import java.util.Set;
      
      import org.openqa.selenium.JavascriptExecutor;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.chrome.ChromeDriver;
      import org.openqa.selenium.chrome.ChromeOptions;
      import org.openqa.selenium.support.ui.ExpectedConditions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class NewTab_url_forLoop {
      
          public static void main(String[] args) {
      
              System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
              ChromeOptions options = new ChromeOptions();
              options.addArguments("start-maximized");
              WebDriver driver =  new ChromeDriver(options); 
              String url1 = "https://mail.google.com/";
              String url2 = "https://www.facebook.com/";
              driver.get(url1);
              String firstWindowHandle = driver.getWindowHandle();
              System.out.println("First Window Handle is: "+firstWindowHandle);
              // Opening Facebook in the adjacent TAB
              ((JavascriptExecutor)driver).executeScript("window.open('" + url2 +"');");
              new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
              Set<String> allWindowHandles = driver.getWindowHandles();
              // Using iterator
              Iterator<String> itr = allWindowHandles.iterator();
              while(itr.hasNext()) {
                  String nextWindow = itr.next();
                  if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
                      driver.switchTo().window(nextWindow);
                      System.out.println("New Tab Window Handle is: "+nextWindow);
                  }
              }
          }
      }
      
    • Console Output:

      First Window Handle is: CDwindow-01F5622275A2EA2C1ABE2F0CDEB3D09B
      New Tab Window Handle is: CDwindow-9E3349B91FB2FA4D5B7D4A90D0E87BD3
      
    • Browser Snapshot:

    0 讨论(0)
  • 2020-11-30 04:12

    I have tried other techniques, but none of them worked, also no error produced, but when I have 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");
    
    0 讨论(0)
  • 2020-11-30 04:12

    To switch between tabs action class does not always works on all browser and on all webdriver. the best way is to use robot class. try this code.

            String website = "https://www.google.com";
            String website1 = "https://www.msn.com/en-in/";
            String controlpath = "C:\\Libraries\\msedgedriver.exe";
            System.setProperty("webdriver.edge.driver", controlpath);
            driver = new EdgeDriver();
            driver.manage().window().maximize(); //  Maximize browser
            driver.get(website);
            System.out.println("Google page");
            
            Robot robot = new Robot();                          
            robot.keyPress(KeyEvent.VK_CONTROL); 
            robot.keyPress(KeyEvent.VK_T); 
            robot.keyRelease(KeyEvent.VK_CONTROL); 
            robot.keyRelease(KeyEvent.VK_T);
            
            //Switch focus to new tab
            ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
            //System.out.println("Handle info"+ driver.getWindowHandles());
            driver.switchTo().window(tabs.get(1));
        
            //Launch URL in the new tab
            driver.get(website1);
            System.out.println("msn page");
            Thread.sleep(5000);
            driver.switchTo().window(tabs.get(0));
            Thread.sleep(5000);
    
    0 讨论(0)
  • 2020-11-30 04:14

    The original poster is asking on how to open a link on a new tab. So this is how I have done it in C#.

            IWebDriver driver = new ChromeDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://microsoft.com");
            IWebElement eventlink = driver.FindElement(By.Id("uhfLogo"));
            Actions action = new Actions(driver);
            action.KeyDown(Keys.Control).MoveToElement(eventlink).Click().Perform(); 
    

    This actually performs a Control+Click on the selected element in order to open the link in a new tab.

    0 讨论(0)
提交回复
热议问题