How to open a new tab using Selenium WebDriver?

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

    Handling browser window using Selenium Webdriver:

    String winHandleBefore = driver.getWindowHandle();
    
    for(String winHandle : driver.getWindowHandles())  // Switch to new opened window
    {
        driver.switchTo().window(winHandle);
    }
    
    driver.switchTo().window(winHandleBefore);   // move to previously opened window
    
    0 讨论(0)
  • 2020-11-22 05:31

    The code below will open the link in new Tab.

    String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
    driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
    

    The code below will open empty new Tab.

    String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
    driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
    
    0 讨论(0)
  • 2020-11-22 05:31

    Why not do this

    driver.ExecuteScript("window.open('your url','_blank');");
    
    0 讨论(0)
  • 2020-11-22 05:31

    How to open a new tab using Selenium WebDriver with Java for chrome?

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.navigate().to("https://google.com");     
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_T);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_T);
    

    Above code will disable first extensions and using robot class new tab will open.

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

    Below code will open the link in new window

    String selectAll = Keys.chord(Keys.SHIFT,Keys.RETURN);
    driver.findElement(By.linkText("linkname")).sendKeys(selectAll);
    
    0 讨论(0)
  • 2020-11-22 05:34

    How to open a new, but more importantly, how do you do stuff in that new tab? Webdriver doesn't add a new WindowHandle for each tab, and only has control of the first tab. So, after selecting a new tab (Control + Tab Number) set .DefaultContent() on the driver to define the visible tab as the one you're going to do work on.

    Visual Basic

    Dim driver = New WebDriver("Firefox", BaseUrl)
    
    ' Open new tab - send Control T
    Dim body As IWebElement = driver.FindElement(By.TagName("body"))
    body.SendKeys(Keys.Control + "t")
    
    ' Go to a URL in that tab
    driver.GoToUrl("YourURL")
    
    
    ' Assuming you have m tabs open, go to tab n by sending Control + n
    body.SendKeys(Keys.Control + n.ToString())
    
    ' Now set the visible tab as the drivers default content.
    driver.SwitchTo().DefaultContent()
    
    0 讨论(0)
提交回复
热议问题