代码之家  ›  专栏  ›  技术社区  ›  Sameer Arora

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

  •  0
  • Sameer Arora  · 技术社区  · 7 年前

    当一个网页上已经打开了一个/多个窗口时,我们可以使用selenium找出已经打开的窗口数。但是,有没有办法通过任何标签或任何其他方式来确定使用selenium可以在给定网页上打开多少个窗口。

    例如,我们对网页上的所有URL都有一个锚定标签,所以有没有办法确定一个网页上可以打开多少个窗口,或者单击多少按钮/链接,就会打开一个窗口。

    任何java或python解决方案,以及任何网页都将不胜感激。

    3 回复  |  直到 7 年前
        1
  •  3
  •   undetected Selenium    7 年前

    直截了当地回答,事实上没有确切的计算方法 使用Selenium可以从网页打开多少(子)窗口 .

    <a> :锚元素

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

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

      • 举个例子:

        <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 除非 测试环境 有充足的资源 记忆力 , 共享内存 等。你可以在 unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium

    笔记 :一个重要的方面是,如果你正在打开一个新的选项卡/窗口,并打算切换 硒的 关注新打开的选项卡/窗口,您需要 WebDriverWait 详情如下:

    • ( JAVA (示例) 预期条件 像 numberOfWindowsToBe(int expectedNumberOfWindows) :

      new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
      
    • ( python (示例) 预期条件 像 number_of_windows_to_be(num_windows) :

      WebDriverWait(driver, 10).until(expected_conditions.number_of_windows_to_be(2))
      
        2
  •  0
  •   Ali    7 年前

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

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

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

    Set<String> windows = driver.getWindowHandles();
    System.out.println("=> The total windows opened is/are : "+windows.size());
    
        3
  •  0
  •   Bhavesh Soni    7 年前

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

    为了做到这一点,您可以使用下面的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); 
    	}
    }