2016-05-24 13 views
-3

私はインターネットで検索しようとしましたが、上記の問題の解決方法を見つけることはできませんでした。 以下のコードを使用してidの値を取得できると思います。 driver.getAttribute( "id");「ID」属性を含むWebページのすべてのWeb要素の数を取得するにはどうすればよいですか?

どのようにすべてのIDを取得するのですか? 私のテストの目的スクリプトは、ウェブページ内のどの要素も重複していないことを確認することです。 Javaでコードが必要です。

+0

idを持つタグの総数を属性の1つとして数えたいと思うようになりましたが、今度はこれらのid属性の値を検索したいのですが、それは正しいですか? –

+0

もしそうなら、私は2つの異なるIDが同じ値を持っているとは思わないが、とにかく –

答えて

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

'id'を持つすべての要素を取得するには、xpathにcontainsを使用し、型に関係なく要素を受け入れるだけです。

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

//here '' is an empty char 

int size = lst.size(); 

私はあなたには、いくつかの問題の解決策をfindoutしようとしていることを信じて、あなたがfacing..definitelyあなたです全体の問題/課題を投稿することができれば、あなたは、ID発行を除く他のすべてで行われ願っていますいくつかの明快さを取得します...

おかげ

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(); 
関連する問題