Skip to main content
blog title image

1 minute read - Selenium WebDriver FAQs Test Automation

How to Select a Web Element using CSS when id containing '.'

Jul 1, 2020

TLDR: Selecting an id with a fullstop or period in it requires a little escaping with CSS selectors. \.


I was working with a site and trying to select an element via its id e.g.

    <button id="">Select Me By id</p>

This is not as simple as:


Try with XPath, and that works fine:

  • //button[@id='']

CSS Misunderstanding

In CSS we are not saying, “find the id

We are saying “find the id select with class me by id” which is not at all what we want.

Fortunately CSS offers escaping.

Locate the id using CSS with:

  • #select\.me\.by\.id

Of course, you could use XPath instead.


    public void cssWithoutEscapingFails(){

        driver.get("" +

        Assertions.assertThrows(NoSuchElementException.class, () ->

    public void cssEscapingExample(){

        driver.get("" +

        String goodSelector = "#select\\.me\\.by\\.id";
        final WebElement button = driver.findElement(

        Assertions.assertEquals("Event Triggered",
                driver.findElement(By.cssSelector(goodSelector + "status")).

Full Source

The full source for this is in my Webdriver Java FAQs project:


If you want to learn how to use Selenium WebDriver with Java then check out our online courses.