2013-05-14 132 views
1

我当前正在尝试使用Selenium WebDriver测试对话框,但“创建员工”按钮没有ID或名称。这就是代码的样子。Java/SeleniumWebDriver - 使用Selenium点击一个JQuery对话框按钮

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"> 
    <div class="ui-dialog-buttonset"> 

     <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"> 
       <span class="ui-button-text">Create Employee</span> 
     </button> 

     <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"> 
       <span class="ui-button-text">Cancel</span> 
     </button> 

    </div> 

     <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"> 
       <span class="ui-button-text">Create Employee</span> 
     </button> 

     <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"> 
       <span class="ui-button-text">Cancel</span> 
     </button> 

</div> 

任何人都可以提供的java代码,将点击对话框中的按钮?由于

+0

嘿,我猜有同名为“创建雇员”我说得对两个按钮? – Omkar 2013-05-14 07:22:30

回答

4

请尝试下面的代码。

driver.findelement(By.xpath("//button[@type='button']/span[text()='Create Employee']")).click(); 
+0

非常感谢!此代码完美工作。 – user2357236 2013-05-14 07:48:57

-1

如果你的文本不会更改,您可以使用:

$('.ui-button-text').click(function() { 
    if($(this).text() == 'Create Employee') { 
     // Your code here 
    } 
}); 

Demo

+0

我已经有了代码。我只需要使用Selenium WebDriver来测试它以提交表单。 – user2357236 2013-05-14 07:21:26

0

如果你没有任何办法选择任何元素我会建议使用xpath 我建议在浏览器上安装一些插件或扩展程序,以获取要交互的元素的xpath。然后使用该xpath为您的目的。

1

作为xpath的替代品,我发现新手很麻烦,我发现jQueryUI对话框可以将ID添加到结果中。

相反的:

$("#stuff").dialog({ 
    buttons: { 
      "Yes": function() { 

写:从上面的html页面

$("#stuff").dialog({ 
    buttons: { 
      "Yes": { text: "Yes", 
        id: "#mystuff-yes", 
        click: function() { 
相关问题