如何使用Selenium检查可以从一个网页中打开多少个窗口

时间:2019-02-04 09:32:23

标签: java python selenium selenium-webdriver window-handles

当网页上已经打开单个/多个窗口时,我们可以使用硒来找出已经打开的窗口数量。但是,是否有任何方法可以通过任何标签或其他任何方法来找出使用硒在给定网页上实际可以打开多少个窗口。

例如,我们为网页上存在的所有URL提供了一个定位标记,因此有什么方法可以找出网页上可以打开多少个窗口或单击多少个按钮/链接,一个窗口将被打开。

使用Java或python以及任何网页的解决方案都将受到赞赏。

3 个答案:

答案 0 :(得分:2)

直接回答,实际上,没有确定的方法来计算可以使用Selenium从网页中打开多少个(子)窗口

<a>:锚元素

HTML <a> element(或 anchor 元素)定义了一个超链接,用于将一个页面链接到其他网页,文件,同一页面中的位置,电子邮件地址或任何其他内容。其他网址。 <a>元素最重要的属性是 href 属性,它指示链接的目的地。按照以下方式,目标属性只能与锚标记中的href属性一起使用:

  • 如果未使用target属性,则链接将在同一页面中打开。

    • 一个例子:

      <a href="https://www.w3schools.com">Visit W3Schools.com!</a>
      
  • 如果目标属性设置为_blank,则该链接将在新的浏览器窗口或新的标签页中打开。

    • 一个例子:

      <!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> 
      

现在,您可以通过HTML Tag触发新的TAB / Windows,直到 Test Environment 拥有足够的 memory 资源>共享内存等。您可以在unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium

中找到相关的讨论

注意:一个重要方面是,如果您要打开新的TAB /窗口并打算将 Selenium的焦点切换到新打开的TAB / Window,则需要 WebDriverWait 如下:

  • Java 示例)期望的条件numberOfWindowsToBe(int expectedNumberOfWindows)

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

    WebDriverWait(driver, 10).until(expected_conditions.number_of_windows_to_be(2))
    

答案 1 :(得分:0)

是的,根据锚标记,您可以在页面中找到可以打开的链接数量,并且可以在“ n”个窗口中打开链接,因此在Windows中打开链接后,您可以获得打开的窗口数,请尝试下面的代码:

false

上面的代码将计算存在的“ href”数量,但是由于可以打开“ n”个窗口,因此无法告诉您可以打开多少个窗口。通过使用以下代码,您可以找到打开的窗口数:

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...");
    }
}

答案 2 :(得分:0)

我基本上得到了您的问题,您想检查一个网页可以打开多少个窗口。

为此,您可以在xpath下面使用它,它会为您提供将在当前网页上打开的网页数

//a[@target="_blank"]

您可以使用如下代码:

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