简体   繁体   中英

How to switch to new tab using selenium C# for Safari Browser

I want to switch to new tab opened by clicked link in Safari browser for MACOS.

Code which is used for Chrome is not working for Safari browser.

driver.SwitchTo().Window(driver.WindowHandles.Last());

tried below code but not working- 1. driver.FindElementExistByWait(By.CssSelector("body")).SendKeys(Keys.Command + "\t"); 2. driver.FindElementExistByWait(By.CssSelector("body")).SendKeys(Keys.Control + "\t");

Can you please help to resolve this issue.

This is kind of a two part process.....

Switch to new tab:

    public static string SwitchToTab()
    {
        var mainHandle = Driver.CurrentWindowHandle;
        var handles = Driver.WindowHandles;

        foreach (var handle in handles)
        {
            if (mainHandle == handle)
            {
                continue;
            }
            Driver.SwitchTo().Window(handle);
            break;
        }
        var result = Url;
        return result;
    }

Then switch to main content if necessary:

           public static void CloseNewTab()
    {
        var mainHandle = Driver.CurrentWindowHandle;
        var handles = Driver.WindowHandles;
        foreach (var handle in handles)
        {
            if (mainHandle == handle)
            {
                continue;
            }
            Driver.SwitchTo().Window(handle);
            Driver.Close();
            Driver.SwitchTo().Window(mainHandle);
            break;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM