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

83qze16e  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(367)

当一个网页上已经打开了一个或多个窗口时,我们可以使用selenium找出已经打开的窗口数。但是,有没有任何方法可以通过任何标签或任何其他方式来确定使用selenium可以在给定的网页上实际打开多少个窗口呢。
例如,我们有一个锚定标签为所有的网址,这是目前在一个网页上,所以有没有办法找出多少窗口可以打开一个网页或点击多少按钮/链接,一个窗口将被打开。
在任何java或python和任何网页的解决方案将不胜感激。

oo7oh9g9

oo7oh9g91#

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

:锚定元件

html <a> 元素(或锚元素)定义一个超链接,用于从一个页面链接到其他网页、文件、同一页面中的位置、电子邮件地址或任何其他url。最重要的属性 <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标签触发新标签/窗口的打开,除非测试环境有足够的内存,共享内存,等等。您可以在未知错误中找到相关的讨论:会话已删除,因为未知错误导致页面崩溃:无法确定从chromedriver selenium崩溃的选项卡加载状态
注意:一个重要的方面是,如果您正在打开一个新的选项卡/窗口,并打算将selenium的焦点切换到新打开的选项卡/窗口,则需要按如下方式诱导webdriverwait:
(java示例)expectedconditions为 numberOfWindowsToBe(int expectedNumberOfWindows) :

new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));

在这里你可以找到一个详细的讨论
(python示例)预期的\u条件为 number_of_windows_to_be(num_windows) :

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

在这里你可以找到一个详细的讨论

wljmcqd8

wljmcqd82#

是的,根据锚定标记,您可以找到可以从页面打开的链接数,并且可以在“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());
ukdjmx9f

ukdjmx9f3#

我得到你的问题基本上你想看看有多少窗口可以打开一个网页。
为了做到这一点,你可以使用下面的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); 
	}
}

相关问题