以下是用于找出弹出窗口中存在的总元素的代码,当我执行代码时,每次返回元素的总数等于1.但实际上弹出窗口中存在多于1个元素。 / p>
有谁能告诉我如何从弹出窗口中获取确切数量的元素?
我们可以处理动态弹出窗口吗?如果是,那么我们如何在运行时获取动态元素?
例如:
在弹出窗口中,总共有四个元素(两个复选框和两个文本字段)。并且假设Admin已从后端删除了两个字段,现在前端总共有两个字段。但是我已经编写了四个字段的代码,所以如果运行代码,它显然会在控制台中显示错误信息,即NoSuchElementFound
。
所以我的问题我们如何处理这种情况?请帮忙。
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.webdriven.commands.GetAttribute;
public class dynamicform {
public static WebDriver driver;
public void Dynamicform() throws InterruptedException {
//Launch Google Chrome Browser
driver = new FirefoxDriver();
driver.get(" "); // Redirect to the Website(put any website link)
driver.findElement(By.cssSelector("#ContentPlaceHolder1_Repeater2_btnSPrequest_1")).click(); //Click on the popup button
Thread.sleep(2000);
driver.findElement(By.id("ContentPlaceHolder1_Control6-0_txtquestion")).sendKeys("asdasd");//sending text to any particular text field
//Listing all the popup forms elements
List<WebElement> forms = driver.findElements(By.cssSelector(".modal-popup"));
//print the Total pop up Elements
int count = forms.size();
System.out.println("Total No. of Elements Present" + count);
//Run the advance for loop
for (WebElement ele : forms) {
//Print the pop up elements "id"
System.out.println(ele.getAttribute("id"));
//Print the pop up elements text
System.out.println(ele.getText());
}
}
public static void main(String[] args) throws InterruptedException {
// Creating the object of the class
dynamicform obj3 = new dynamicform();
// call the Dynamicform method
obj3.Dynamicform();
// Closing the Driver
driver.close();
}
}