0

When single/multiple windows are already opened on a webpage, we can find out the number of windows which are already opened by using selenium. But is there any way to find out by any tag or any other way that how many total windows can actually be opened on a give webpage using selenium.

For example, we have an anchor tag for all the URLs which are present on a webpage, so is there any way to find out how many windows can be opened on a webpage or on clicking how many buttons/links, a window will be opened.

Solution in any of java or python and for any webpage would be appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20

3 Answers3

3

Answering straight, factually there is no definite way to calculate how many (child) windows can be opened from a webpage using Selenium.

<a>: The Anchor element

The HTML <a> element (or anchor element) defines a hyperlink which is used to link from one page to other web pages, files, locations within the same page, email addresses, or any other URL. The most important attribute of the <a> element is the href attribute, which indicates the link's destination. The target attribute can only use with href attribute in anchor tag as per the following ways:

  • If the target attribute isn't used then link will open in same page.

    • An example:

      <a href="https://www.w3schools.com">Visit W3Schools.com!</a>
      
  • If the target attribute is set to _blank, the link will open in a new browser window or a new tab.

    • An example:

      <!DOCTYPE html>  
      <html>  
          <head>  
              <title></title>  
          </head>  
          <body>  
              <p>Click on <a href="https://www.javatpoint.com/" target="_blank"> this-link </a>to go on home page of JavaTpoint.</p>  
          </body>  
      </html> 
      

Now you can trigger the opening of new TABs/Windows through the HTML Tag untill and unless the Test Environment have ample resources interms of memory, shared memory, etc. You can find a related discussion in unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium

Note: One important aspect is if you are opening a new TAB/Window and intend to switch Selenium's focus to the newly opened TAB/Window, you need to induce WebDriverWait as follows:

  • (Java Example) ExpectedConditions as numberOfWindowsToBe(int expectedNumberOfWindows):

    new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
    
  • (Python Example) expected_conditions as number_of_windows_to_be(num_windows):

    WebDriverWait(driver, 10).until(expected_conditions.number_of_windows_to_be(2))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you Debanjan for sharing your valuable insights on it and i was quite familiar with most of these. The window number question was just out of curiosity that is that also possible in selenium, as i and my team were discussing something in the lunchtime and this question came up so i thought i should ask on stackoverflow to check if thats possible or not, which now seems is not possible, though thank you again for sharing your insights on the same. – Sameer Arora Feb 05 '19 at 06:01
0

Yes, based on the anchor tag you can find how many links you can open from the page and a link can be opened in 'n' windows so after opening the link in windows you can get the opened windows count, try the below code :

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Testing {
    public static void main(String ...ali) {
        System.setProperty("webdriver.chrome.driver", "C:\\NotBackedUp\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("alicse3"+Keys.ENTER);

        List<WebElement> links = driver.findElements(By.xpath("//a"));
        int nonEmptyLinks = 0;
        for(WebElement element : links) {
            String link = element.getText().trim();
            if(!link.isEmpty()) {
                System.out.println(element.getText());
                nonEmptyLinks++;
            }
        }
        System.out.println("=> The total links is/are '"+nonEmptyLinks+"', so you can open '"+nonEmptyLinks+"' windows using anchor tag...");
    }
}

The above code will count how many 'href' are present but can't tell how many windows you can open because you can open 'n' number of windows. By using the below code, you can find the opened windows count :

Set<String> windows = driver.getWindowHandles();
System.out.println("=> The total windows opened is/are : "+windows.size());
Ali
  • 1,689
  • 1
  • 5
  • 12
  • Thanks but my question is not how many links can be opened, its how many windows can be opened on the webpage. – Sameer Arora Feb 04 '19 at 10:10
  • You can open 'n' number of windows – Ali Feb 04 '19 at 10:12
  • If you want count of opened windows then you can use the window handles – Ali Feb 04 '19 at 10:13
  • Thanks Ali but i have already mentioned that i know how to find now many windows are already opened on the webpage, i want to know how many windows can be opened on that page or on clicking how many links, a window will be opened on that page. – Sameer Arora Feb 04 '19 at 10:14
  • Can you show me some examples with some web page so that may be I can try the answer for your question – Ali Feb 04 '19 at 10:16
  • The thing is, you can open 1 link in 'n' number of windows so you can't say exactly the windows count because it depends on the user/something who opens how many links – Ali Feb 04 '19 at 10:18
  • What you are saying are tabs, i am taking about a window, which is different. – Sameer Arora Feb 04 '19 at 10:22
  • A tab is more or less same as a window. A window can contain several tabs and all session data and cookies are shared across all tabs and open window. [Refer This Link](https://www.google.com/search?q=windows+vs+tabs%3F&oq=windows+vs+tabs%3F&aqs=chrome..69i57.2856j0j1&sourceid=chrome&ie=UTF-8) – Ali Feb 04 '19 at 10:23
0

I got your question basically you would like to check how many window can be opened by a webpage.

In order to do this you can use below xpath which will give you count of webpage will be opened on current webpage

//a[@target="_blank"]

you can use code like below:

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class Testing {
 public static WebDriver driver;

 @Test
 public void test() throws InterruptedException {
  
  
  
  System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver");
  driver = new ChromeDriver();
  driver.get("https://stackoverflow.com");
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
  Thread.sleep(1000);
  List<WebElement> elements = driver.findElements(By.xpath("//a[@target=\"_blank\"]"));
  int count =0;
  for ( WebElement element:elements) 
  {
   count++;
  
  }
  System.out.println("Total number of window will be opened by this webpage is:"+ count); 
 }
}
Bhavesh Soni
  • 188
  • 8
  • I have a doubt in your code, you are printing everything a loop, so there would be multiple outputs and not only one. Though I have tried you code on https://grofers.com/ and i got 16 rows and outputs were something like (1200, 30) and (53, 16)..... – Sameer Arora Feb 04 '19 at 11:00
  • I have updated the code snippet. You can try the above code for the same. It will give you the result what you are looking for. – Bhavesh Soni Feb 04 '19 at 11:25
  • Have tried you xpath on grofers.com, it locates the element which opens on a new tab like Share button for twitter, facebook. But what i want is to check the number of windows which can get opened on the page like it does after clicking on My account and then Login button. – Sameer Arora Feb 04 '19 at 11:58