如何获取包含“ID”属性的网页中所有网页元素的计数?

时间:2016-05-24 10:32:10

标签: java selenium selenium-webdriver

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

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

5 个答案:

答案 0 :(得分:0)

要获取具有' id'的所有元素,您只需在xpath中使用contains和' *'接受任何元素,无论其类型如何。

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

//here '' is an empty char 

int size = lst.size();

我相信你正试图找到某个问题的解决方案,并希望你完成除了id问题以外的所有其他事情,如果你能发布你所面临的整个问题/挑战..无疑你会得到一些清晰的...

由于

答案 1 :(得分:0)

嗨,请尝试如下

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

答案 2 :(得分:0)

快速代码

    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();

答案 3 :(得分:0)

如果使用的是POM Framework,则可以尝试以下代码;

//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());
    }

答案 4 :(得分:-1)

计算并打印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());

}
}