seleniumlover
seleniumlover

Reputation: 159

How do I use regular expressions in between the CSS Selenium Selector code?

I am having trouble to use the regular expression in between the unique element id.

Here's the html code

<td align="left" style="vertical-align: top; ">   
 <span class ="radiobutton"   id="mainId:gen_id_12e3:radiospan1e">      
   <input type="radio" name="radio1" value="on" id="radio1" tabindex="0" checked>       
     <label for="id-2024">Yes</label>

Using xpath, i used the selenium command below with (.*) in between the element id which works fine.

    selenium.click("xpath=//span[@id, 'mainId.*radiospan1e']");

However, I can't do the same in CSS as they have $ for the end of the id, ^ for the beginning and * at anywhere in the page but i want the id starting with mainId with some regular expression and ends with the unique id.

Any help would be appreciated.

Upvotes: 4

Views: 14169

Answers (1)

BoltClock
BoltClock

Reputation: 723598

CSS doesn't support regular expressions in general, but if you just want to wildcard the entire middle part out of your ID, that's actually quite doable in CSS, as shown in this answer.

Given your code, you can use:

selenium.click("css=span[id^='mainId'][id$='radiospan1e']");

Upvotes: 7

Related Questions