开发者

selenium.windowfocus() what is this command used for?

what is thi开发者_StackOverflows command for?


You might think that Selenium.selectWindow() would be all you need. But that simply tells Selenium which window you want all the Selenium commands to go to. One of the commands you can send to it is "give this (currently selected) window focus".

It's a bit confusing, because Windows (and other systems) sometimes refer to the "selected window" - the one that's on top of the others, or the "active" window. Here, we call it the window that "has focus". It's the window where keyboard events will be directed. Inside a window, individual widgets (text fields, scroll bars, buttons) can have focus too.

So windowFocus() is like clicking on the title bar of the window that Selenium is currently working with.


From the Selenium Documentation

windowFocus()

Gives focus to the currently selected window


Unless or until, you are using windowHandles to switch between multiple windows, ur focus will be default on first windows launched by selenium. widnowFocus does the same thing


In my experience, getting window focus using the Selenium windowFocus() method is sometimes not effective. I find myself sometimes using a JavascriptExecutor, then use the Selenium switchTo() method to switch to the handle that needs focus and then execute :

public static void getWindowFocus( String windowHandle ) {
   driver.switchTo( windowHandle );
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript( "window.focus();" );
    js = null;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜