How to open a new tab using Selenium WebDriver?

后端 未结 29 2983
野的像风
野的像风 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条回答
  •  猫巷女王i
    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()
    

提交回复
热议问题