1
votes
<div id="temp_1333021214801"> 
    <input type="text"/> 
</div>

$browser.text_field(:xpath,".//*[@id='temp_1333018770709']/input").set("apple")

I am getting error "unable to locate element", because the ID changes dynamically.

Please help me to set the text in the text field.

2
You need to give us the HTML of the page, and more detail about which locators for that element are static and which are dynamic. A link to the site would be ideal. - adam reed
Your "my html code" doesn't show any HTML. All we have is your Ruby code. No HTML for this sort of problem makes me cry, then my girlfriend has to console me, it can get messy and sometimes I miss dinner. So please, for the sake of my nutrition. - kinofrost
SHOW us your HTML from the webpage you are trying to automate. Stop using xpath unless it's the only way. Also I see zero here related to cucumber, why is it even in the title and how does that relate to your issue? - Chuck van der Linden
You need to give us more HTML than just the one element (with a non static ID) that you are trying to identify. We want to help you but you MUST give us enough to work with so that we can do that. Enough that we might be able to find you some pattern regarding some other thing around or near the div you want that would be predictable (such as perhaps a title or label for the field?) HTML for the entire form the field is part of might be best. - Chuck van der Linden

2 Answers

0
votes

It seems like your dynamic id is temp_ so this should do it given information above:

browser.div(:id, /temp_\d+/).text_field.set 'something'

Issues with my solution is that it assumes id will always be temp_ regex matching any number set consecutively, which seems to be the case with your sample above. Also, it assumes there is no other div(:id, /temp_\d+/) combination in the DOM of that page, most likely should not be an issue.

0
votes

If you have dynamic IDs I can suggest the following:

  1. Code to object counts. For example

    $browser.text_field(:index => 2)

    gives the third text_field on the page.

  2. Code to what is around the thing you're trying to find.

    $browser.div(:name => 'mydiv').text_field(:index=>2)

    gives the third text field in the div called 'mydiv'.

HOWEVER

If your front-end is less-than-testable in this way I highly suggest you put time into thinking over your commitment to automated testing in the first place. Any minor change to the software is going to have you working until 9pm pulling your hair out and rocking back and forth as you update all your scripts, so unless code maintenance is your weekend hobby think about semi-automation or exploratory testing or manual scripts. Talk to development (whomever that might be. It might be you!) or the higher-ups (unless that's you too) to see if it can be made more testable. Also don't use xpaths unless you take some deviant pleasure in it.

Hope that was helpful, I can't do anything specific without the source HTML.