4

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.

1 Answer 1

7

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']");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Boltclock. Clever way of using regular expressions in CSS :).
Just to be clear, as BoltClock says, CSS doesn't do regular expressions at all. $= is a tail-string match, that's all. Similarly *= is a partial-substring match, and ^= is a head-string match.
Not working for me for selecting a div like this: <div class="matchCardMain_954a1"></div> with this driver.find_element_by_xpath("css=div[class^='matchCardMain_'][class$='1']")
@ishandutta2007: Because you're using find_element_by_xpath() and not find_element_by_css_selector().
@BoltClock sure , can you make your answer more explicit specifying that, as it is not clear. So for xpath's case should it be driver.find_element_by_xpath("xpath=...") ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.