Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6169605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:53:08+00:00 2026-05-23T22:53:08+00:00

I was working on reading mails from gmail using webdriver and in between I

  • 0

I was working on reading mails from gmail using webdriver and in between I hit upon this difference between By.id and By.tagname.

I am trying to get access to a “table” whose id is “:pg”. So I could

  1. Either use By.id(“:pg”)
  2. OR use By.tagname(“table”) and search for an element with id :pg

Here is the code for both cases.

By.id:

WebDriver webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver = webDriver.switchTo().frame("canvas_frame");
WebElement table1 = webDriver.findElement(By.id(":pg"));`

Above code, I directly get the element which has id “:pg”

By.tagname:

WebDriver webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> tables = webDriver.findElements(By.tagName("table"));
for(WebElement table2: tables){
    String id = table2.getAttribute("id");
    System.out.println("id: "+ id);
    if(id != null && id.equals(":pg")){
        System.out.println("FOUND IT!!!");
    }
}

Above code, I find all elements with the tagname of table and then see which one has the id “:pg”.

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)

Why is this difference between By.id and By.tagname?

Thanks,
Chris.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:53:09+00:00Added an answer on May 23, 2026 at 10:53 pm

    The :pg element is not present on the page initially.

    Using By.Tag, selenium will not wait for the :pg element.

    Because By.Id example is more specific, selenium will continue checking if the :pg element exists until the implicit wait (5 seconds) times out.

    By.Tag is not specific at all. On findElements(By.tagName("table"), Selenium will return an array of all the tables that are present immediately after the page loads. As the :pg element is not present yet, it will not be in the array.

    To answer your question, yes it is better to use By.Id because:
    1. It is more specific.
    2. Saves lines of code
    3. Forces selenium to wait for the element to exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After some reading from this tutorial , I have the following partly working code:
Clarification Upon working through your answers and reading your interpretations of this question, I
I'm currently working on a project trying to update a page reading from an
I am working on a android project that can read e-mails from any gmail
reading excel files from C# working well in 32 bit version server. It is
I`m working on an app that does reading and handling specific URIs from NFC
Reading this MSDN article titled Working with ObjectSet (Entity Framework) It shows two examples
I am working on task involving reading from the socket trading quotes and I
I am working on a basic_streambuf to handle reading and writing from/to a Winsock
I am working on a log parsing script using Perl. I am reading the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.