title: Is by.id better than by.tagname?
body: <p>I was working on reading mails from gmail using webdriver and in between I hit upon this difference between By.id and By.tagname. </p>
  
  <p>I am trying to get access to a "table" whose id is ":pg". So I could </p>
  
  <ol>
  <li>Either use By.id(":pg") </li>
  <li>OR use By.tagname("table") and search for an element with id :pg</li>
  </ol>
  
  <p>Here is the code for both cases.</p>
  
  <p>By.id:</p>
  
  <pre><code>WebDriver webDriver = new FirefoxDriver();
  webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  webDriver = webDriver.switchTo().frame("canvas_frame");
  WebElement table1 = webDriver.findElement(By.id(":pg"));`
  </code></pre>
  
  <p>Above code, I directly get the element which has id ":pg"</p>
  
  <p>By.tagname:</p>
  
  <pre><code>WebDriver webDriver = new FirefoxDriver();
  webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  List&lt;WebElement&gt; tables = webDriver.findElements(By.tagName("table"));
  for(WebElement table2: tables){
      String id = table2.getAttribute("id");
      System.out.println("id: "+ id);
      if(id != null &amp;&amp; id.equals(":pg")){
          System.out.println("FOUND IT!!!");
      }
  }
  </code></pre>
  
  <p>Above code, I find all elements with the tagname of table and then see which one has the id ":pg".</p>
  
  <p>Both these code snippets are essentially doing the same but using different ways(By.id or By.tagname). However, the first snippet of code which uses By.id always succeeds while the second snippet of code which uses By.tagname fails almost always. (It will work with additional waiting however)</p>
  
  <p>Why is this difference between By.id and By.tagname? </p>
  
  <p>Thanks,
  Chris.</p>