【问题标题】:How to open a new tab using Selenium WebDriver and start the link?如何使用 Selenium WebDriver 打开一个新选项卡并启动链接?
【发布时间】:2014-05-25 13:32:51
【问题描述】:

如何使用 Selenium WebDriver 打开新标签页?

我想在新标签页中打开多个链接。这是为了尽快完成构建验证任务。因此,在每个新选项卡中都可以打开所有与烟雾测试相关的链接,然后在每个对应于烟雾测试要求的选项卡中,我们可以进行健全性测试。

【问题讨论】:

标签: selenium webdriver


【解决方案1】:

我们可以使用 WebDriver 的 Actions 类。见以下代码:

WebDriver driver = new FirefoxDriver();
driver.get("<provide URL>");
WebElement link = driver.findElement(locator);
Actions builder = new Actions(driver);
Action openLinkInNewTab = builder
         .moveToElement(link)
         .sendKeys(link, Keys.CONTROL)
         .click(link)
         .keyUp(Keys.CONTROL)
         .build();

openLinkInNewTab.perform();

这可以循环用于多个链接。

【讨论】:

  • 如果你已经将鼠标移动到元素上,那么当你click()它时你不需要提供元素。使用sendKeys() 不会做任何事情,因为当您运行click() 时,控制键已停止按下。然后当您调用 keyUp() 时,它会崩溃,因为 Control 键已经启动。
【解决方案2】:

/* 在浏览器中打开新标签 */

public void openNewTab()

{
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs.get(0));

}

【讨论】:

    【解决方案3】:

    在新标签页中打开链接的唯一方法是模拟键盘快捷键。以下适用于 FFX、Chrome 和 IE

    1. Ctrl+t 将打开一个空白的新标签,并将焦点切换到它。
    2. 按住 Ctrl,然后单击链接将在新选项卡中打开链接,但将焦点留在现有选项卡上。
    3. 按住 Ctrl 和 Shift,然后单击将在新选项卡中打开链接并将焦点移到新选项卡上。
    4. Ctrl+w 将关闭当前选项卡并将焦点切换到最后打开的选项卡(尽管请注意 Ctrl+W 即 Ctrl+Shift+w 将关闭所有选项卡!)

    Selenium(当前)在浏览器窗口中没有任何选项卡的概念,因此要打开选项卡然后对其进行测试,您必须使用选项 3。

    以下代码将执行选项 3。然后立即关闭该新选项卡。 (在 C# 中)

    new Actions(WebDriver)
        .KeyDown(Keys.Control)
        .KeyDown(Keys.Shift)
        .Click(tab)
        .KeyUp(Keys.Shift)
        .KeyUp(Keys.Control)
        .Perform();
    
    new Actions(WebDriver)
        .SendKeys(Keys.Control + "w")
        .Perform();
    

    你也可以使用:

    .MoveToElement(tab)
    .Click()
    

    在第一个选项的中间,和

    .KeyDown(Keys.Control)
    .KeyDown("w")
    .KeyUp("w")
    .KeyUp(Keys.Control)
    

    在第二个。

    【讨论】:

    • 我尝试了选项 3,它在新选项卡中打开链接,它也会打开一个新的空白窗口
    【解决方案4】:

    代码:

    WebDriver wd = new FirefoxDriver();
    wd.get("http://www.gmail.com");
    
    wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);    
    wd.manage().window().maximize();
    //To open a new tab         
    Robot r = new Robot();                          
    r.keyPress(KeyEvent.VK_CONTROL); 
    r.keyPress(KeyEvent.VK_T); 
    r.keyRelease(KeyEvent.VK_CONTROL); 
    r.keyRelease(KeyEvent.VK_T);    
    //To switch to the new tab
    ArrayList<String> tabs = new ArrayList<String>(wd.getWindowHandles());
    wd.switchTo().window(tabs.get(1));
    //To navigate to new link/URL in 2nd new tab
    wd.get("http://facebook.com");
    

    【讨论】:

    • 上面的机器人代码仅适用于我打开一个新标签。但是第二个 URL (facebook.com) 没有在第二个选项卡中打开,因为它没有切换到第二个选项卡。它需要切换到第二个标签
    • 代码输入错误。将driver. 替换为wd.。由于某种原因,编辑被拒绝。
    【解决方案5】:

    上述解决方案对我不起作用。不知道为什么,但是驱动程序会导航到新的 url,但是一个新的标签不会打开。

    我终于设法使用此代码打开了新标签:

    IWebDriver driver = new ChromeDriver("path for chromedriver.exe");
    IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
    driver.Navigate().GoToUrl("url1");
    js.ExecuteScript("window.open()");
    driver.SwitchTo().Window(driver.WindowHandles[driver.WindowHandles.Count - 1]);
    driver.Navigate().GoToUrl("url2");
    

    driver.WindowHandles.Count - 1 将为您提供最后打开的选项卡,即此场景中的新选项卡。希望这可以帮助某人。

    【讨论】:

      【解决方案6】:
      import java.util.concurrent.TimeUnit;
      import org.openqa.selenium.By;
      import org.openqa.selenium.Keys;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.testng.annotations.BeforeTest;
      import org.testng.annotations.Test; 
      
      import java.awt.Robot;
      import java.awt.event.KeyEvent;
      
      import java.awt.AWTException; 
      
      
      
      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(); 
      
      
      
        //perform required actions on tab 1.
        driver.findElement(By.xpath("//input[@id='6']")).click();
        driver.findElement(By.xpath("//input[@id='plus']"));
        driver.findElement(By.xpath("//input[@id='3']"));
        driver.findElement(By.xpath("//input[@id='equals']"));
      
        //Call switchToTab() method to switch to 2nd tab.
        switchToTab();
      
        //Call switchToTab() method to switch to 1st 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 BLOG POST

      【讨论】:

        【解决方案7】:
        // To open a new tab to establish second player connection
                ((JavascriptExecutor)driver).executeScript("window.open('about:blank', '-blank')");
                // To switch to the new tab
                ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
                driver.switchTo().window(tabs.get(1));
                // To navigate to new link/URL in 2nd new tab
                driver.get("http://localhost:8080");
        

        我做到了,效果很好!

        【讨论】:

          【解决方案8】:

          在第一个解决方案中,添加一个 Thread.sleep(5000) 语句。在添加 sleep 语句之前,我多次出现错误 Index out of bounds。

          WebDriver wd = new FirefoxDriver();
          wd.get("http://www.gmail.com");
          
          wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);    
          wd.manage().window().maximize();
          //To open a new tab         
          Robot r = new Robot();                          
          r.keyPress(KeyEvent.VK_CONTROL); 
          r.keyPress(KeyEvent.VK_T); 
          r.keyRelease(KeyEvent.VK_CONTROL); 
          r.keyRelease(KeyEvent.VK_T);    
          
          try {Thread.sleep(5000);} catch (InterruptedException e) {}
          
          //To switch to the new tab
          ArrayList<String> tabs = new ArrayList<String>(wd.getWindowHandles());
          wd.switchTo().window(tabs.get(1));
          //To navigate to new link/URL in 2nd new tab
          wd.get("http://facebook.com");
          

          【讨论】:

            猜你喜欢
            • 2016-04-22
            • 1970-01-01
            • 2018-02-12
            • 2021-12-21
            • 2013-07-07
            • 1970-01-01
            • 2014-04-24
            • 2016-05-06
            • 1970-01-01
            相关资源
            最近更新 更多