【问题标题】:Able to Switch the driver but not the focus能够切换驱动程序但不能切换焦点
【发布时间】:2016-12-07 08:50:34
【问题描述】:

场景:假设我有两个选项卡 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;
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • “标签焦点没有改变”是什么意思?到底发生了什么?
  • 假设我在 TAB1 并且我想要我的焦点(从用户视图现在活动选项卡是 TAB1)。现在我希望我的驱动程序和焦点切换到 TAB2。当我使用 driver.switchto ().window(windowhandle2),驱动程序正在切换到 TAB2 但从用户视图仍然显示在 TAB1
  • 您需要更多信息吗?

标签: javascript c# selenium-webdriver


【解决方案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();

【讨论】:

  • 它不起作用。实际上“driver.switchTo()”正在切换我的驱动程序,但不是选项卡的焦点。
  • @PrathyushaDwibhashi ,试试这个来集中注意力
  • JavascriptExecutor jse = (JavascriptExecutor) 驱动程序; jse.executeScript("document.getElementById('YourControlID').focus()");
【解决方案2】:

我尝试将 JavaScript 与 window.focus() 一起使用,但它不起作用。PFB 代码。

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;
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多