Home > Enterprise >  Windows vs tabs in Selenium
Windows vs tabs in Selenium

Time:03-02

As per Difference between webdriver.Dispose(), .Close() and .Quit() driver.close closes the current active window where driver.quit closes all of the windows. What I get is if I opened a new tab in my current window and then called driver.close the whole windows including my tabs would close. But when I run that code

driver.get("http://testingpool.com/selenium-ide-part-1/");
driver.findElement(By.xpath("//*[@id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()

only the first tabs gets closed. Also I find that my allWindowHandles has length of 2 although I have only one window. I was trying to open a new window using Selenium as per https://www.tutorialspoint.com/how-to-open-a-new-window-on-a-browser-using-selenium-webdriver-for-python using

((JavascriptExecutor)driver).executeScript("window.open('')");

but that resulted in a new tab not a new window.

I am confused if Selenium differentiates between a tab and a window at all or not.

CodePudding user response:

With the terminology used to distinguish between driver.close() and driver.quit() methods supported by Selenium WebDriver in the post you are referencing to, they actually mostly mean browser tabs, not windows.
By opening a new browser window with

((JavascriptExecutor)driver).executeScript("window.open('')");

or by clicking on some web element opening a new browser tab/window is will normally open a new tab in the existing browser.

CodePudding user response:

What I get is if I opened a new tab in my current window and then called driver.close the whole window including my tabs would close. But when I run that code

See you may have opened a new tab by clicking on link or using JavascriptExecutor but Selenium still has focus on first tab of the first window.

Therefore, when you do this:

ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close() 

It should close the first tab or window, since you have not switched to new tab/window.

Also I find that my allWindowHandles has length of 2

this is cause you have opened a new tab using this line

driver.findElement(By.xpath("//*[@id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab

the moment you open a new tab by clicking on a link or Using JavascriptExecutor, you will have 1 windows handle.

However, if you do not switch to any tab or window/s. Selenium will always have focus on first tab of the first window.

Also, window.open('') is Js command not Selenium.

there is a difference between a new tab or windows. But if you take Selenium into consideration, We switch to new tab or windows in a same way.

However to open a new window, you would have to simulate CTRL N keyboard action.

  • Related