3 回答

TA贡献1842条经验 获得超22个赞
直接回答,事实上没有明确的方法来计算可以使用 Selenium 从网页打开多少(子)窗口。
<a>: 锚元素
HTML<a>元素(或锚元素)定义了一个超链接,用于从一个页面链接到其他网页、文件、同一页面内的位置、电子邮件地址或任何其他 URL。<a>元素最重要的属性是href属性,它指示链接的目的地。target 属性只能与锚标签中的 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 标记触发新 TAB/Windows 的打开,除非测试环境在内存、共享内存等方面有足够的资源。您可以在未知错误中找到相关讨论:会话因页面崩溃而被删除未知错误:无法从 ChromeDriver Selenium 崩溃的选项卡中确定加载状态
注意:一个重要的方面是,如果您正在打开一个新的 TAB/Window 并打算将Selenium 的焦点切换到新打开的 TAB/Window,则需要如下诱导WebDriverWait :
(Java示例)ExpectedConditions为numberOfWindowsToBe(int expectedNumberOfWindows):
new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
(Python示例)expected_conditions为number_of_windows_to_be(num_windows):
WebDriverWait(driver, 10).until(expected_conditions.number_of_windows_to_be(2))

TA贡献1829条经验 获得超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);
}
}

TA贡献2037条经验 获得超6个赞
是的,根据锚标记,您可以找到可以从页面打开的链接数量,并且可以在“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...");
}
}
上面的代码将计算存在多少“href”,但无法确定您可以打开多少个窗口,因为您可以打开“n”个窗口。通过使用以下代码,您可以找到打开的窗口数:
Set<String> windows = driver.getWindowHandles();
System.out.println("=> The total windows opened is/are : "+windows.size());
添加回答
举报