Java 如何获取包含“0”的网页中所有web元素的计数;ID";属性

Java 如何获取包含“0”的网页中所有web元素的计数;ID";属性,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我曾尝试在互联网上搜索,但无法找到任何解决上述问题的方法。 我想我们可以使用下面的代码得到id的值。 driver.getAttribute(“id”) 但是如何捕获所有ID?? 我的测试脚本的目标是确认网页中没有重复id的元素。 Java中需要代码。要获取所有具有“id”的元素,您只需使用xpath中的contains和“*”来接受任何元素,而不管其类型如何 List<WebElement> lst = driver.findElements(By.xpath("//*[

我曾尝试在互联网上搜索,但无法找到任何解决上述问题的方法。 我想我们可以使用下面的代码得到id的值。 driver.getAttribute(“id”)

但是如何捕获所有ID?? 我的测试脚本的目标是确认网页中没有重复id的元素。
Java中需要代码。

要获取所有具有“id”的元素,您只需使用xpath中的contains和“*”来接受任何元素,而不管其类型如何

List<WebElement> lst = driver.findElements(By.xpath("//*[contains(@id,'') ]"));

//here '' is an empty char 

int size = lst.size();
List lst=driver.findElements(By.xpath(“/*[contains(@id,”)]);
//此处“”是一个空字符
int size=lst.size();

我相信你正在试图找到解决某个问题的方法,希望除了身份证问题之外,其他的一切都能解决,如果你能发布你所面临的整个问题/挑战……你肯定会得到一些清晰的答案……

嗨,请像下面这样尝试

// opening Firefox Browser
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// for simplicity i have used below URl as solution
driver.get("http://docs.seleniumhq.org/");

// take each and every tag which have id attribute inside the list
List<WebElement> myTagsWithId = driver.findElements(By.cssSelector("[id]"));
// if in case you want to work with xpath please use By.xpath("//*[@id]")

// Print the size of the tags
System.out.println("Total tags with id as one of the attribute is : " + myTagsWithId.size());

// now printing all id values one by one
for(int i =0;i<myTagsWithId.size();i++){
System.out.println("Id Value is : " + myTagsWithId.get(i).getAttribute("id"));
        }
//打开Firefox浏览器
WebDriver=newfirefoxdriver();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
//为了简单起见,我使用下面的URl作为解决方案
驱动程序。获取(“http://docs.seleniumhq.org/");
//获取列表中每个具有id属性的标记
List myTagsWithId=driver.findElements(By.cssSelector(“[id]”);
//如果您想使用xpath,请使用By.xpath(“/*[@id]”)
//打印标签的大小
System.out.println(“id作为属性之一的总标记为:“+myTagsWithId.size());
//现在逐一打印所有id值
对于(int i=0;i快速代码

    WebDriver driver = new FirefoxDriver();
    driver.get("page_that_you_want_to_check");

    List<WebElement> lst = driver.findElements(By.xpath("//*[contains(@id,'') ]"));

    Map<String, Integer> checkList = new HashMap(); 

    for(WebElement ele : lst) {
        String currentId = ele.getAttribute("id");
        if(!currentId.equals(null) && !currentId.equals("")) {
            if(checkList.containsKey(currentId)) {
                checkList.put(currentId, checkList.get(currentId)+1);
            }
            else checkList.put(currentId, 1);
        }
    }

    for (String key : checkList.keySet()) {
        if(checkList.get(key) > 1) {
            System.out.println("There are: " + checkList.get(key) + " with ID " + key);
        }
    }
    driver.quit();
WebDriver=newfirefoxdriver();
获取(“你想要检查的页面”);
List lst=driver.findElements(By.xpath(“/*[contains(@id,”)]);
映射检查表=新的HashMap();
for(WebElement ele:lst){
字符串currentId=ele.getAttribute(“id”);
如果(!currentId.equals(null)和&!currentId.equals(“”){
if(检查表容器(当前ID)){
检查表.put(currentId,checkList.get(currentId)+1);
}
else检查表。put(当前ID,1);
}
}
for(字符串键:checkList.keySet()){
如果(检查表获取(键)>1){
System.out.println(“有:“+checkList.get(key)+”和ID“+key”);
}
}
driver.quit();

如果您使用的是POM框架,您可以尝试下面的代码

//get total number of products available and print its name from a[anchor tag] in POM Framework
@FindBy(xpath="//div[@class='product_grid_display group']//div[@class='grid_product_info']//a")
private List<WebElement> AllProductName;
public void GetAllProductWithCount()
{
    // take each and every tag which have a [anchor] attribute inside the list
    List <WebElement> TotalProducCount = AllProductName;
    // Print the size of the tags
    int size =TotalProducCount.size();
    System.out.println("Total Products are : "+size );
    // now printing all anchor tag values one by one
    for(int i=0;i<size;i++)
    {
        System.out.println("TotalProducCount is "+TotalProducCount.get(i).getText());
    }
//获取可用产品的总数,并从POM框架中的[anchor tag]打印其名称
@FindBy(xpath=“//div[@class='product\u grid\u display group']//div[@class='grid\u product\u info']//a”)
私有列表AllProductName;
public void GetAllProductWithCount()
{
//获取列表中每个具有[anchor]属性的标记
列出TotalProducCount=AllProductName;
//打印标签的大小
int size=TotalProducCount.size();
System.out.println(“总产品为:“+尺寸”);
//现在逐个打印所有锚定标记值

对于(int i=0;i计算并打印google.com上存在的超链接总数

package p1;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CountUrl {
     public static void main(String[] args) {
         System.setProperty("webdriver.gecko.driver","/home/mcastudent/Downloads/software/geckodriver" );
         WebDriver driver=new FirefoxDriver();
         driver.get("https://www.google.com");
         //List<WebElement> links = driver.findElements(By.xpath("//a"));
         List<WebElement> links = driver.findElements(By.tagName("a"));
         System.out.println("No. of links: "+links.size());

}
}
p1包;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类CountUrl{
公共静态void main(字符串[]args){
setProperty(“webdriver.gecko.driver”,“/home/mcastuent/Downloads/software/geckodriver”);
WebDriver=newfirefoxdriver();
驱动程序。获取(“https://www.google.com");
//List links=driver.findElements(By.xpath(“//a”);
列表链接=driver.findElements(按.tagName(“a”));
System.out.println(“链接数:+links.size());
}
}

让我重新表述一下,您想计算id作为属性之一的标签总数,现在您想找到这些id属性的值,对吗?如果是,那么我认为两个不同的id不会有相同的值,但无论如何