Reputation: 1
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.
Upvotes: 0
Views: 932
Reputation: 1551
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;
}
}
Upvotes: 0