Switch between tabs in Mozilla using Selenium - java

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

Related

How to switch focus between windows using WinAppDriver Java

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());

Selenium in new tabs

I'm currently working on Selenium tests for a web platform, and I've noticed that the test have problems finding elements on a certain page which will make the test fail. The problem occurs once the test have pressed a button, which opens up a new tab, so my question is whether or not the reason for this happening is due to the webdriver being set to the first tab in the webbrowser or is it something else?
PS.The test has no problem finding elements if the driver is set to start on the second page.
You have to switch to a new tab, because driver would find only the actual tab WebElements.
Switch to a new tab by using:
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.close(); //to close actual tab
driver.switchTo().window(tabs.get(1)); //then switch to new tab
Your suggestion sounds correct, to work with the new tab you have to switch driver to it.
This can be simply done like this:
public void switchToNewWindow(){
wait(500);
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
}
to switch back to the first window you can use this:
public void switchToFirstWindow(){
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
}
In case there are more than 2 tabs you can switch to any tab as shown above switching to tab with the corresponding index.

Selenium- After click a link,IE opens two windows instead of one window

I'm working on the secured web application. When I click link within frame, it opened another single window where information to be filled.But when I execute this scenario in selenium, it click the link within frame and system display two windows where window1 shows Blank page with title as "Blank Page- window internet explorer' and window2 shows website security certificate with no title.
When I'm doing manually, it showing single window but during automation, it shows two windows.
Note: Application support only IE10.
script:
System.setProperty("webdriver.ie.driver","./tools/IEDriverServer_32.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability("ignoreZoomSetting", true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get(url);
driver.navigate().to("javascript:document.getElementById('overridelink').click()");
Login the application and next step to click link
driver.findElement(By.xpath(".//table[#id='maintable']//a").click();
Please help me on this.
I encountered the exact same issue in IE 10..The issue appears to be resolved when I am setting "nativeEvents" to true using the DesiredCapabilities Class. You may try the same and let us know if it also works for you. Please find code segments for your reference:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability("nativeEvents", true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
The 2nd Line seems to do the trick.
The below solution worked
Modifying the registry value , TabProcGrowth to 0 solved the issue-
Go to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
TabProcGrowth (right-click) → Modify… → Value data: 0
which version of selenium jar are you using. Try below code...
System.setProperty("webdriver.ie.driver","./tools/IEDriverServer_32.exe"); WebDriver driver = new InternetExplorerDriver();
driver.get(url); driver.navigate().to("javascript:document.getElementById('overridelink').click()‌​");
if this not works.... last option, please Reinstall IE and problme will be fixed.

Bring the Firefox Browser to Front using selenium Java (Mac OSX)

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().

How to open new tab and enter address into it using webdriver

I want to open www.google.com in one tab and then open new tab and open www.facebook.com
I am using Below code but it open facebook in first tab
driver.get("www.google.com");
driver.findElement(By.tagName("Body")).sendKeys(keys.CONTROL + "t");//opens new tab
driver.get("www.facebook.com");//but load facebook in first tab i.e on google page
Is this because I am using same name driver.get??
If the code to open the new tab worked, you can use driver.switchTo().window() to switch to the newly opened tab (as seen here).
But If the tab didn't open, Selenium does not provide any mechanism to do this, so you have to implement around it (like using the java.awt.Robot class)
You should also consider just using two different instances of the WebDriver and run both your sites in its own window
Use java awt Robot class as below.
driver.get("http://www.google.com");
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
driver.get("http://www.facebook.com");

Categories