简体   繁体   中英

ruby watir web-driver can't access dynamic elements

I have a site I need to login to but one of the input text fields id and name change each time. Is there a way to access element via a regex? Thanks in advance.

Example:

id="form:wrap:j_idt1297:0:j_idt1298:input"
id="form:wrap:j_idt2151:0:j_idt2152:input"

<input class="iceInpSecrt large" 
id="form:wrap:j_idt1297:0:j_idt1298:input"    
name="form:wrap:j_idt1297:0:j_idt1298:input" 
onblur="setFocus('');" onfocus="setFocus(this.id);" 
onkeyup="iceSubmit(form,this,event);" 
onmousedown="this.focus();" tabindex="" type="password" value="">

Yes, you can match elements using regular expressions. It is similar to locating the id/name by string.

It looks like:

browser.text_field(:id => /a_regex/)

For your example, the following would locate the text field with either of the ids mentioned:

browser.text_field(:id => /form:wrap:j_idt\d{4}:0:j_idt\d{4}:input/)

Note:

  • You will need to verify that this regex will not end up also matching other elements on the page. If it does, you will need to make your search more specific by using other information about the element (perhaps the class) or information around the element (perhaps the field's label).
  • Depending on the other elements on the page, this regex might be more specific than you need. For example, perhaps just the first part is unique. In that case you could just do /form:wrap:j_idt/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM