Regex – selenium regular expression such as id=regexp:.* doesn’t work

regexselenium

I have an .aspx page and I'm trying to locate a textbox via the selenium UI. The id is: p_lt_ctl01_pageplaceholder_p_lt_ctl00_SignUpFree_txtFirstName

I've tried: id=*._txtFirstName
and: id=glob:*_txtFirstName

Is there a better way other than CSS to locate a textbox whose id may change each time it's compiled?

Best Answer

You can't put wildcards in an id "selector". Either you use id=whole_id_here or don't.

Fortunately, for your case, you can use the CSS selector:

[id*=_txtFirstName]

In Selenium IDE, use it like:

css=[id*=_txtFirstName]

Example Selenium IDE source snippet:

<tr>
    <td>storeText</td>
    <td>css=[id*=_txtFirstName]</td>
    <td>x</td>
</tr>
<tr>
    <td>echo</td>
    <td>${x}</td>
    <td></td>
</tr>



Note: If _txtFirstName is always at the end, you can also use the CSS locator with $ instead of * (it is more restrictive, will only match if it is at the end, while * matches if it is anywhere): [id$=_txtFirstName].           (In Selenium IDE, naturally, use it like: css=[id$=_txtFirstName].)