How to Select a Web Element with an id containing ‘.’

TLDR: \.

Lesson learned.

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

<p id="select.me.by.id">Select Me By My id</p>

Simples thought me:

  • #select.me.by.id

Nope.

So I did what all good Selenium debuggers do… I tried in the browser but Chrome didn’t match it.

Tempted though I was to blame Chrome, I tried with XPath, and that worked fine:

  • //p[@id='select.me.by.id']

After a bit of web searching, the solution was clear.

In CSS I wasn’t saying, find the id select.me.by.id

I was saying find the id select with class me by id which was not at all what I wanted.

Fortunately CSS offers escaping, which I didn’t realise.

Until the app is fixed I can locate the id using CSS with:

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

This entry was posted in FAQ, Selenium Simplified. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *