1

I'm trying to capture the id of an element that will be randomly generated. I can successfully capture the value of my element id like this...

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

Now my variable will be something like...

divElement-12345

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

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

How might I be able to accomplish this?

IAdapter
  • 62,595
  • 73
  • 179
  • 242
Andrew
  • 227,796
  • 193
  • 515
  • 708

2 Answers2

4

You have two options in Selenium, XPath and CSS Selector. I have read that CSS Selector is better for doing tests in both FireFox and IE. Using the latest version of Selenium IDE (3/5/2009) I've had success with 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
s_hewitt
  • 4,252
  • 24
  • 24
  • 2
    I'd also recommend that if you are trying to do this level of complexity with your scripts, you seriously consider using Selenium RC. – Patrick Lightbody Jun 15 '09 at 13:12
1

There are many functions in XPATH which should solve your problem. Assuming "divElement-" is a constant that will not change and that you are using XPath 2.0, I would suggest:

substring-after(div[1]/@id/text(),"divElement-")

Tirno
  • 1,568
  • 2
  • 16
  • 21
  • I tried every variation of this that I could think of, but I couldn't get this to work. I don't think Selenium supports XPath 2.0. =/ – Andrew Feb 23 '09 at 18:37