开发者

Selenium problem locating by DOM

开发者 https://www.devze.com 2023-01-02 04:53 出处:网络
I\'m trying to use the DOM to locate a form element in Selenium but I can\'t get it to work. Even if I use the example in the Selenium documentation it still fails, for example with this html...

I'm trying to use the DOM to locate a form element in Selenium but I can't get it to work. Even if I use the example in the Selenium documentation it still fails, for example with this html...

 <html>
  <body>
   <form id="loginForm">
 开发者_运维技巧   <input name="username" type="text" />
    <input name="password" type="password" />
    <input name="continue" type="submit" value="Login" />
    <input name="continue" type="button" value="Clear" />
   </form>
 </body>
 <html>

and this command in the Selenium IDE...

verifyElementPresent 

with target...

 dom=document.forms['loginForm']

I get [error] false in the log. The 'getElementById' example in the documentation does work, but none of the others.

Can someone explain what I'm doing wrong here? Thanks.


Not sure why it's not working (I can replicate the problem), but perhaps there's a better way to locate your target element? I would recommend locating by ID/name, falling back to CSS or XPath.


The format to locate a element is-

i) document.forms[index of the form].elements[index of the element]

index of the form = the index number (starting at 0) of the form with respect to the whole page, index of the element = the index number (starting at 0) of the element with respect to the whole form that contains it.

ii) document.forms[“name of the form”].elements[“name of the element”]

name of the form = the value of the name attribute of the form tag that contains the element you want to access, name of the element = the value of the name attribute of the element you wish to access

iii) document.getElementById(“id of the element”)

id of the element = this is the value of the ID attribute of the element to be accessed. This value should always be enclosed in a pair of parentheses (“”).

iv)document.getElementsByName(“name”)[index]

name = name of the element as defined by its ‘name’ attribute, index = an integer that indicates which element within getElementsByName’s array will be used.

0

精彩评论

暂无评论...
验证码 换一张
取 消