Selenium how to access two controls of same css class

我们两清 提交于 2019-12-01 12:42:04

问题


I am doing testing using selenium ide. My objective is to verify the following

1) Max and Min length property of text boxes. 2) Verify the texts of the labels

My html code is as below:

<div class="control-group">
                                <label class="control-label" for="input01">Email</label>
                                    <div class="controls">
                                        <input name="data[Salon][username]" class="span4" id="username" placeholder="Username or Email" type="text"/>                                   
                                    </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Password</label>
                                    <div class="controls">
                                        <input name="data[Salon][password]" class="span4" id="password" placeholder="Required" type="password"/>                                                                    
</div>

But in the above I am facing following problems:

a) I have problem in accessing the labels for assertText or assertElementPresent since they are having same class name.

b) I don't know how verify the Max and Min length property of the Text boxes.

And please note that when I am trying to use

document.getElementsByClassName("control-label")

I am getting the following error:

[error] locator not found: document.getElementsByClassName("control-lable"), error = TypeError: e.scrollIntoView is not a function

回答1:


You can access first label via:

css=div[class='control-group'] label[class='control-label']:contains('Email')

You can access second lavel via:

css=div[class='control-group'] label[class='control-label']:contains('Password')

Use these with command assertElementPresent, "contains" in element locator allows you to verify text in it.

Also you can use xpath:

//div[@class='control-group']//label[@class='control-label'][text()='Email']
//div[@class='control-group']//label[@class='control-label'][text()='Password']

Usually maxlength property is set as attribute of the input, but i can't see it in your html code.. But you can try:

storeAttribute (Selenium IDE's command) and as target you can use xpath:

/div[@class='control-group']//label[@class='control-label'][text()='Email']/@maxlength
save it to some var (eg set to the value field smthg like attLength) and then echo this var like: Selenium IDE's command echo and as put to the target field ${attLength}

来源:https://stackoverflow.com/questions/25657401/selenium-how-to-access-two-controls-of-same-css-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!