How do you select elements in selenium that have a period in their id?

css-selectors, selenium, selenium-webdriver, webdriver

Solution

There is incredibly bad development practice. I know you don't have the ability to change it, but if you can, point out that it's very very bad. Why? Two reasons.

In CSS, the rules based on classes generally start with a period.

In CSS selector frameworks, including jQuery/Sizzle and what Selenium is doing in this example, the period has a special meaning - mainly to select elements based on many rules. This is why it's tripping here here - you can see the same thing if you run the CSS selector direct into Chrome or Firebug - it will fall over as well.

Using a period in the ID for your elements is going against all this. Annoyingly the HTML spec allows for this.

Anyway, all is not lost, there are many ways around it.

First, you can escape it:

select#param\\.Status

Second, you could use a slightly more elaborate selector:

select[id='param.Status']

Finally, you could use XPath:

//select[@id='param.Status']

Problem

I am trying to write code that will select all options for an HTML drop-down menu. I have written the following code which I believe should work. ``` public void testSelectMultipleOptions () { // code to get to report page selectAllOptions("param.Status"); // code to run report and switch to the result page } public void selectAllOptions(String htmlID) { List<WebElement> options = selenium.findElements(By.cssSelector("select#"+htmlID+" > option")); for(WebElement option: options) { option.click(); } } ``` When I run this code no options are selected in the drop-down. I believe the problem I am having is caused by the fact that I have an HTML element with a period in the id but I do not have the ability to change the underling HTML code for the page.

Original source