I am new to windows automation using win app driver.
Our application is developed with chromium browser and we are using win app diver to automate since the main app we are trying to open is windows based.
When I click on Ok button opens another window(Window B). I have 2 windows opened window 1 and window 2. I need to perform actions on both the windows for that I need to shift the focus between two windows. When I use getwindowhandles() method I am getting number of windows opened as 1.
How can I switch between windows using winapp driver.
Appreciate your help.
Thanks
I am using in my code:
this.driver.SwitchTo().Window(this.driver.WindowHandles[0]);
However, I do not expect this to work in your case, as your number of open windows is 1, than means that there is no second window to switch to.
So in your case you can use root session in order to attach to your window:
AppiumOptions rootSessionOptions = new AppiumOptions();
rootSessionOptions.AddAdditionalCapability("app", "Root");
rootSessionOptions.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), rootSessionOptions);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
var VSWindow = _driver.FindElementByName("Your project name without .csproj - Microsoft Visual Studio");
var VSTopLevelWindowHandle = VSWindow.GetAttribute("NativeWindowHandle");
VSTopLevelWindowHandle = (int.Parse(VSTopLevelWindowHandle)).ToString("x");
AppiumOptions VisualStudioSessionOptions = new AppiumOptions();
VisualStudioSessionOptions.AddAdditionalCapability("appTopLevelWindow", VSTopLevelWindowHandle);
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), VisualStudioSessionOptions);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
Reference:
https://github.com/microsoft/WinAppDriver/issues/978
OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle solution
This code works for me (windows automation using win app driver) with C#
//Switch to the next window in desktop application:
IList<string> toWindowHandles = new List<string>(_driver.WindowHandles);
Thread.Sleep(6000);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
With Java:
Thread.sleep(5000);
//Switch to the next window in desktop application:
Set<String> windowHandles = driver.getWindowHandles();
driver.switchTo().window(windowHandles.iterator().next());
Related
I have a situation where I need to open a link incognito window from another url.
I have tried by using action class to perform control shift n key events but it's not working.
Robot class working fine but I'm looking for other alternative.
Actions act = new Actions(driver);
act. KeyDown(Keys.cONTROL);
act.keyDown(Keys.SHIFT);
act.keyDown("n").build(). perform ();
But it's not working
In case you want to open a new window in incognito mode i.e. without cookies you can simply open a regular new Selenium driver new window and the to clear the cookies with the following command:
driver.manage().deleteAllCookies();
I am using Selenium with Java, and I am using this code to switch between tabs in Mozilla, but it is opening a new window instead of a new tab. How do solve this, or is there another way to switch between tabs?
WebDriver shiva=new FirefoxDriver();
shiva.manage().window().maximize();
shiva.get("http://www.naukri.com/");
Thread.sleep(3000);
shiva.findElement(By.xpath("/html/body/div[1]/div/ul[1]/li[2]/a")).click();
shiva.findElement(By.xpath("/html/body/div[1]/div/ul[1]/li[1]/a")).sendKeys(Keys.CONTROL +"\t");
Try to use this code:
ArrayList<String> tabs = new ArrayList<>(webDriver.getWindowHandles());
webDriver.switchTo().window(tabs.get(1)); // id of the tab
In a normal firefox window, a new window is opened in a new tab, if you go to settings --> General --> Tabs you will see an option Open new window in new tab instead
But when Selenium Webdriver launches a firefox profile, this option isn't selected by default so it opens in a new window instead of a new tab.
If you want to open a new tab, you need to create a different firefox profile with this option enabled and then you can launch the created profile
There is another way by which you can switch to a different tab.
Set<String> listOfTabs = driver.getWindowHandles();
// This code will return a set with all the window ids
// Then you can switch on any of the window.
driver.switchTo.window("String Id of window");
Your code should be below for switching between Tabs: Assert the relevant tab based on pageTitle (I Guess)
shiva.findElement(By.xpath("/html/body/div[1]/div/ul[1]/li[1]/a")).sendKeys(Keys.CONTROL + Keys.PAGE_DOWN);
If you wish to switch between windows then use
driver.switchto().window(windowhandle);
Also I wonder how are you able to open multiple Tabs?
Use firefox version 80.0 or above
I think it is a bug and it is resolved in firefox 80.0
I too had the same issue(using firefox version- 78.0 back then) but once i changed the version to 80 or above,it worked flawlessly
I'm trying to switch between windows and put an accountant to find out if the Selenium found the second window , but does not find it , note that he sees only 01 window , which is the Father window.
how do I find It the second window and switch to It?
I use: selenium-server-standalone-2.48.2 + Eclipse + Java 1.8
I tried this:
//Get all window handles
Set<String> allHandles = driver.getWindowHandles();
//count the handles Here count is=2
System.out.println("Count of windows:"+allHandles.size());
//Get current handle or default handle
String currentWindowHandle = allHandles.iterator().next();
System.out.println("currentWindow Handle"+currentWindowHandle);
//Remove first/default Handle
allHandles.remove(allHandles.iterator().next());
//get the last Window Handle
String lastHandle = allHandles.iterator().next();
System.out.println("last window handle"+lastHandle);
//switch to second/last window, because we know there are only two windows 1-parent window 2-other window(ad window)
driver.switchTo().window(lastHandle);
System.out.println(driver.getTitle());
driver.findElement(By.tagName("body")).click();
message on the console:
I faced similar issue with IE 11. Solved it by checking enable protected mode settings for all zones in IE settings.
I am using Selenium webdriver 2.44 with firefox 34 on windows 7 machine. I have a script where I hover over an 'open page' icon and click it. The click opens the a new tab manually and in chrome driver 2.15. The scenario opens a new window instead of tab which I handle in firefox-34. below is the code.
public void switchWindow(){
//try {
String winHandleBefore = driver.getWindowHandle();
System.out.println("before "+winHandleBefore);
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(winHandleBefore)) {
return;
}
}
The problem I am facing is one or two tests run for more than 10,000 seconds as they look like they hang at switching the windows from parent to child. This is issue is reproducible each and every time.Has anybody seen this issue?. Is there a workaround?.Kindly let me know if additional information needs to be provided.
I have fixed this issue by closing the first window and shifting the focus back on the newly opened window. I didn't expect the fix would be this simple.
The fix works for FF-35 as well.
I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.
Please suggest if there any other method for doing the same in firefox.
Store the window handle first in a variable, and then use it to go back to the window later on.
//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();
//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);
Additionally, you can try to maximise the window after switching to it, which should also activate it.
this.webDriver.manage().window().maximize();
Try switching using the window name:
driver.switchTo().window("windowName");
Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp
As described in other topics, you can use
driver.manage().window().setPosition(new Point(-2000, 0));
too.
# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd() # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")
Only thing that worked for me on mac: self.driver.fullscreen_window().