场景:假设我有两个标签TAB1和TAB2。我想根据特定条件在这些选项卡之间切换。使用下面的代码,我可以在选项卡之间切换驱动程序,但选项卡焦点不会改变。
帮我解决这个问题。有没有可能实现这个目标?
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;
}
}
}
}
}
}
答案 0 :(得分:1)
如果我没错,你想打开子窗口。 试试这个,希望这会对你有所帮助
//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();
答案 1 :(得分:0)
我尝试在window.focus()中使用JavaScript,但它没有用.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(WebDriverUtilities3.WebDriver.Driver.WindowHandles); if(currentWindow_title!= title) { foreach(availableWindows中的字符串w) { if(currenhandle!= w) { 。WebDriverUtilities3.WebDriver.Driver.SwitchTo()窗口(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;
}
}
}
}