Selenium WebDriver Locating Strategies: Using CSS Selectors with Tag and ID

Learn to use CSS selectors combining tag name and ID attributes for precise and efficient web element location in Selenium WebDriver. This tutorial provides code examples and best practices for creating robust and reliable automated tests.



Selenium WebDriver Locating Strategies: CSS Selectors with Tag and ID

CSS selectors provide a concise and efficient way to locate HTML elements in Selenium. Combining the tag name with the ID attribute in a CSS selector creates a very specific and robust locator. This approach is generally more efficient and reliable than using only the tag name or the ID attribute alone. This is because it directly targets the element based on its unique identifier (the ID) and also its type (the tag name).

CSS Selector Syntax (Tag and ID)

The syntax for a CSS selector combining the tag name and ID is:

CSS Selector Syntax (Tag and ID)

css=tagName#elementId
            

Where:

  • tagName: The HTML tag name of the element (e.g., input, button, div).
  • elementId: The value of the element's id attribute.

Example: Automating Text Entry and Button Click

Let's build a test case that automates entering text into a textbox and clicking a submit button on a webpage (https://www.testandquiz.com/selenium/testing.html). This illustrates how to use CSS selectors for element location and interaction.

  1. Set up your Environment: Make sure you have Selenium WebDriver (and the appropriate WebDriver for your browser, e.g., GeckoDriver for Firefox, ChromeDriver for Chrome) configured correctly. You will need to download the correct WebDriver and set the system property in your Java code to point to the location of the executable. The location of the GeckoDriver or ChromeDriver may vary depending on your system.
  2. Launch Browser: Launch your chosen browser using Selenium.
  3. Launching Firefox
    
    System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    WebDriver driver = new FirefoxDriver(capabilities);
                    
  4. Navigate to URL: Use driver.navigate().to("your_url") to open the webpage.
  5. Navigating to URL
    
    driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
                    
  6. Locate Elements: Inspect the page's HTML to find the textbox's ID (e.g., "fname") and submit button's ID (e.g., "idOfButton").
  7. Interact with Elements: Use driver.findElement(By.cssSelector()) to locate elements and sendKeys() and click() to interact with them:
  8. Interacting with Elements
    
    driver.findElement(By.cssSelector("input#fname")).sendKeys("JavaTpoint");
    driver.findElement(By.cssSelector("button#idOfButton")).click();
                    
  9. Close the browser: `driver.quit()`

Advantages of Using CSS Selectors (Tag and ID)

  • Conciseness: CSS selectors provide a compact way to locate elements.
  • Specificity: Combining the tag name and ID ensures precise targeting.
  • Performance: Generally faster than XPath locators.
  • Maintainability: Relatively less likely to be affected by changes in the webpage’s structure.