Reputation: 157
Scenario: Lets say I have two tabs TAB1 and TAB2. I want to switch between these tabs based on certain criteria. With the below code I'm able to switch the driver between the tabs but tab focus is not getting changed.
Help me on this..Is there any possibility to achieve this?
public static Boolean SwitchWindow(string title)
{
try
{
var currentWindow_title = Driver.Title;
var currenhandle = Driver.CurrentWindowHandle;
var availableWindows = new List<string>Driver.WindowHandles);
if (currentWindow_title != title)
{
foreach (string w in availableWindows)
{
if (currenhandle != w)
{
Driver.SwitchTo().Window(w);
var tit = Driver.Title;
if (Driver.Title == title)
{
break;
}
}
}
}
}
}
Upvotes: 0
Views: 507
Reputation: 157
I tried Using JavaScript with window.focus() and it didnt work.PFB Code.
public static Boolean SwitchWindow(string title)
{
var currentWindow_title = WebDriverUtilities3.WebDriver.Driver.Title;
var currenhandle = WebDriverUtilities3.WebDriver.Driver.CurrentWindowHandle;
var availableWindows = new List<string>(WebDriverUtilities3.WebDriver.Driver.WindowHandles);
if (currentWindow_title != title)
{
foreach (string w in availableWindows)
{
if (currenhandle != w)
{
WebDriverUtilities3.WebDriver.Driver.SwitchTo().Window(w);
IJavaScriptExecutor js = (IJavaScriptExecutor)WebDriverUtilities3.WebDriver.Driver;
string a = @"window.blur();
window.focus();";
try
{
js.ExecuteScript(a);
}
catch (Exception)
{
throw;
}
js = null;
var tit = WebDriverUtilities3.WebDriver.Driver.Title;
if (WebDriverUtilities3.WebDriver.Driver.Title == title)
{
break;
}
}
}
}
}
Upvotes: 0
Reputation: 193
If i am not wrong, you want to switch on child window. Try this, hope this will help for you
//storing parent window reference into string variable
String ParentWindow = driver.getWindowHandle();
//switching from parent to pop up window
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
//implicit wait for visibility of pop up button
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//explicit wait for visibility of pop up button
WebDriverWait wait = new WebDriverWait(driver, 30);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("??")));
driver.findElement(By.xpath("//input[@value='??']")).click();
}
//Switching back to Parent Window
driver.switchTo().window(ParentWindow);
driver.switchTo().defaultContent();
Upvotes: 1