How to capture only part of the ID?

I am trying to grab the id of an element that will be randomly generated. I can successfully grab the value of my item id like this ...

| storeAttribute | //div[1]@id | variableName |

      

Now my variable will be something like ...

divElement-12345

      

I want to remove the "divElement-" so that the variable I left is "12345" so I can use it later to select the associated "form-12345" element ... something like this:

| type | //tr[@id='form-${variableName}']/td/form/fieldset/p[1]/input | Type this |

      

How can i do this?

+1


source to share


2 answers


You have two options in Selenium, XPath and CSS Selector. I read that CSS Selector is better for testing in both FireFox and IE. Using the latest Selenium IDE (3/5/2009) I had success using storeEval which evaluates Javascript expressions giving you access to javascript string functions.

XPath:

storeAttribute | //div[1]@id                                            | divID
storeEval      | '${divID}'.replace("divElement-", "")                  | number
type           | //tr[@id='form-${number}']/td/form/fieldset/p[1]/input | Type this

      



CSS selector:

storeAttribute | css=div[1]@id                                                     | divID
storeEval      | '${divID}'.replace("divElement-", "")                             | number
type           | css=tr[id='form-${number}'] > td > form > fieldset > p[1] > input | Type this

      

+3


source


XPATH has many features that should solve your problem. Assuming "divElement-" is a constant that won't change and that you are using XPath 2.0, I would suggest:



substring-after (Div [1] / @ id / text (), "DivElement -")

+1


source







All Articles