Open link in new tab selenium c#

前端 未结 2 1798
予麋鹿
予麋鹿 2021-01-16 12:24

I am writing a program to run videos listed on my site for testing purpose and here what I need is to run videos in different tabs of the same browser window.

I hav

相关标签:
2条回答
  • 2021-01-16 13:04

    Try this:

    public void SwitchToTab(object pageId)
    {
        webDriver.SwitchTo().Window(pageId.ToString());
    }
    

    You can use CurrentWindowHandle to find current tab.

    webDriver.CurrentWindowHandle;
    

    For your scenario I'm using that code:

    public IPageAdapter OpenNewTab(string url)
    {
        var windowHandles = webDriver.WindowHandles;
        scriptExecutor.ExecuteScript(string.Format("window.open('{0}', '_blank');", url));
        var newWindowHandles = webDriver.WindowHandles;
        var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
        webDriver.SwitchTo().Window(openedWindowHandle);
        return new SeleniumPage(webDriver);
    }
    

    Update

    Window open create new popup. By default this option can be blocked by browser settings. Disable popup blocking in your browser manually.

    To check this, open js console in your browser and try to execute command window.open('http://facebook.com', '_blank');

    If new window open successfully than everythng is OK.

    You can also create your chrome driver with specific setting. Here is my code:

    var chromeDriverService = ChromeDriverService.CreateDefaultService();
    var chromeOptions = new ChromeOptions();
    chromeOptions.AddUserProfilePreference("profile.default_content_settings.popups", 0);
    return new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromSeconds(150));
    
    0 讨论(0)
  • 2021-01-16 13:17

    Here is a simple solution for open a new tab in seleneium c#:

    driver.Url = "http://www.gmail.net";
    IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
    js.ExecuteScript("window.open();");
    
    0 讨论(0)
提交回复
热议问题