在span类中的按钮上运行自动化

时间:2014-05-11 08:47:16

标签: html selenium xpath

我必须使用Selenium准备自动化测试用例以进行网站的注册/登录过程:https://redmart.com/ 使用Firefox的Selenium IDE时,我可以使用xpath找到按钮(单击“查找”按钮,搜索到的按钮会以黄色突出显示)但在执行测试用例期间,无法单击登录按钮或注册按钮。按钮的html代码(可以使用inspect元素查找):

<div id="signin">
<a href="/signup" data-bypass="true" class="signinBtn button">
<span>Log in</span>
</a>
<a href="/signup" data-bypass="true" class="signupBtn button">
<span>Sign up</span>
</a>
</div>

2 个答案:

答案 0 :(得分:0)

试试这个:

open https://redmart.com/
pause 5000
click xpath=//div[@id='signin']/a[1]

IDE提供的注册元素位置是:“css = a.signupBtn.button&gt; span”。它使用xpath的css intead,但它可以工作。

我在Java中尝试了这个代码,它也有效:

@Test
public void test1() {
  driver.get("https://redmart.com/");
  Thread.sleep(2000);
  driver.findElement(By.xpath("//div[@id='signin']/a[1]")).click(); //click to login button
  Thread.sleep(2000);
  driver.findElement(By.xpath("//div[@id='signin']/a[2]")).click(); //click to signup button
  driver.close();
}

答案 1 :(得分:0)

尝试下面的脚本,它工作正常

   <tr>
    <td>open</td>
    <td>http://redmart.com/</td>
    <td></td>
</tr>
<tr>
    <td>verifyText</td>
    <td>css=a.signinBtn.button &gt; span</td>
    <td>Log in</td>
</tr>
<tr>
    <td>verifyText</td>
    <td>css=a.signupBtn.button &gt; span</td>
    <td>Sign up</td>
</tr>
<tr>
    <td>click</td>
    <td>css=a.signinBtn.button &gt; span</td>
    <td></td>
</tr>
<tr>
    <td>pause</td>
    <td>1000</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>name=email</td>
    <td></td>
</tr>
<tr>
    <td>type</td>
    <td>name=email</td>
    <td>test@redmart.com</td>
</tr>
<tr>
    <td>type</td>
    <td>name=password</td>
    <td>1234567</td>
</tr>
<tr>
    <td>verifyText</td>
    <td>css=button.signinBtn.button</td>
    <td>Log In</td>
</tr>
<tr>
    <td>click</td>
    <td>css=button.signinBtn.button</td>
    <td></td>   
</tr>
<tr>
    <td>verifyText</td>
    <td>css=a.signupBtn.button &gt; span</td>
    <td>Sign up</td>
</tr>
<tr>
    <td>click</td>
    <td>css=a.signupBtn.button &gt; span</td>
    <td></td>
</tr>
<tr>
    <td>pause</td>
    <td>1000</td>
    <td></td>
</tr>
<tr>
    <td>verifyText</td>
    <td>id=signupBar</td>
    <td>Three Reasons to Sign Up 1. Easy re-order <br /> with RedMart <br /> 2.             
                 Delivery 7 days a <br /> week in Singapore <br /> 3. Enjoy great       
                   discounts on our products <br /> <br /> Sign Up</td>
</tr>
<tr>
    <td>type</td>
    <td>css=#topBarSignUpForm &gt; input[name=&quot;email&quot;]</td>
    <td>etest</td>
</tr>
<tr>
    <td>type</td>
    <td>css=#topBarSignUpForm &gt; input[name=&quot;password&quot;]</td>
    <td>3432432`</td>
</tr>
<tr>
    <td>type</td>
    <td>name=password_confirm</td>
    <td>324324</td>
</tr>
<tr>
    <td>click</td>
    <td>css=button.signup.button</td>
    <td></td>
</tr>