如果所有按钮

时间:2016-04-26 08:59:22

标签: java xpath selenium-webdriver buttonclick

我的页面中有三个按钮,我需要在不同的时间点击两个按钮。但我无法做到这一点,因为xpath和其他html元素(idclasstypevaluename)是相同的所有这些按钮。如何唯一标识这些按钮以进行单击?请注意,这些按钮位于同一页面的不同位置,需要逐个点击它们。

我试过这样的话:

driver.findElement(By.xpath("(//input[@name='submit'])[2]")).click(); 

并且能够单击一个按钮。但当我尝试使用'3'而不是两个时,没有任何事情发生。 HTML:

<document>
<html class="js" lang="en" dir="ltr" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body class="not-front logged-in page-manage-listing-agent no-sidebars tableHeader-processed">
<!-- wrapper -->
<div id="wrapper">
<!-- container -->
<div id="container">
<!-- page_wrapper -->
<div id="page_wrapper">
<!-- page -->
<div id="page">
<!-- page-header -->
<div id="page-header" class="style_float">
<!--end page-header -->
<div class="clear"/>
<!-- main menu -->
<div class="style_main_menu">
<!--end main menu -->
<div class="clear"/>
<div id="subheading" class="style_flow">
<div class="clear"/>
<!-- center -->
<div id="center" class="style_center_content_default">
<!-- content above -->
<div class="style_content_above_wrapper">
<!-- end content above -->
<!-- left side bar -->
<div id="sidebar-left" class="sidebar">
<!--end left side bar -->
<div class="style_content_main_wrapper">
<div id="content-main">
<!-- error message -->
<div class="error-message "/>
<!-- content help -->
<div class="content-help"/>
<div class="clear-block">
<h3 class="style_sub_title"/>
<form id="rmls-manage-agent-listing" method="post" accept-charset="UTF-8" action="/manage-listing-agent/4740/7/Next">
<div>
<h3 class="style_sub_title">Tour Activity Status</h3>
<input id="edit-step" type="hidden" value="8" name="step"/>
<div class="container-inline-date form-item date-clear-block">
<div class="container-inline-date form-item date-clear-block">
<div class="form-submit-button">
<input id="form-k4S3uBJXVTcXq3Ix1Shkjt3nf-62zmYfOpkAslvJ8tQ" type="hidden" value="form-k4S3uBJXVTcXq3Ix1Shkjt3nf-62zmYfOpkAslvJ8tQ" name="form_build_id"/>
<input id="edit-rmls-manage-agent-listing-form-token" type="hidden" value="f18fc36d4d750ae3912d9cc941aa5c72" name="form_token"/>
<input id="edit-rmls-manage-agent-listing" type="hidden" value="rmls_manage_agent_listing" name="form_id"/>
<table class="sticky-header" style="position: fixed; top: 0px; width: 605px; left: 480px; visibility: hidden;">
<table class="statistic_table sticky-enabled sticky-table">
<div id="style_activity_text" class="style_sub_title_two">Premium Placements</div>
<div id="">
<form id="rmls-activity-report-placement-list-form" method="post" accept-charset="UTF-8" action="/manage-listing-agent/4740/7/Next">
<div>
<div class="placement_save">
<input id="submit" class="form-submit bluebutton" type="submit" value="Save" name="submit"/>
</div>
<div class="placement_div">
<div class="placement_div">
<div class="placement_div">
<div class="placement_div">
<div class="placement_div">
<div class="placement_div">
<div class="placement_div">
<div class="placement_div">
<div class="placement_submit_div">
<input id="submit" class="form-submit bluebutton" type="submit" value="Save" name="submit"/>
</div>
<input id="form-8AJLqZQrgw0cybk8XOkWelCk6iaSJK_7G21QiBLdNKg" type="hidden" value="form-8AJLqZQrgw0cybk8XOkWelCk6iaSJK_7G21QiBLdNKg" name="form_build_id"/>
<input id="edit-rmls-activity-report-placement-list-form-form-token" type="hidden" value="4305d09bc8205b6d0db44711023395c9" name="form_token"/>
<input id="edit-rmls-activity-report-placement-list-form" type="hidden" value="rmls_activity_report_placement_list_form" name="form_id"/>
</div>
</form>
</div>
<div class="style_sub_title_two">Custom Placements</div>
<a class="custom-placement-link" href="/%2523">Add New</a>
<div id="activity_report_placement_form_div" style="display:none;">
<form id="rmls-activity-report-placement-form" enctype="multipart/form-data" method="post" accept-charset="UTF-8" action="/manage-listing-agent/4740/7/Next">
<div>
<div id="placement_title-wrapper" class="form-item">
<div id="placement_link-wrapper" class="form-item">
<div id="edit-thumb-wrapper" class="form-item">
<input id="edit-placement-listing-id" type="hidden" value="4740" name="placement_listing_id"/>
<input id="submit" class="form-submit bluebutton" type="submit" value="Save" name="submit"/>
<input id="form-edEgxWrM-VMdRs75vREPtHbONXyCmJR4KkZRkRoSU2Y" type="hidden" value="form-edEgxWrM-VMdRs75vREPtHbONXyCmJR4KkZRkRoSU2Y" name="form_build_id"/>
<input id="edit-rmls-activity-report-placement-form-form-token" type="hidden" value="185635f8c3122f16a07b5b626e7fc473" name="form_token"/>
<input id="edit-rmls-activity-report-placement-form" type="hidden" value="rmls_activity_report_placement_form" name="form_id"/>
</div>
</form>
</div>
<div id="">
<form id="rmls-activity-report-custom-placement-list-form" method="post" accept-charset="UTF-8" action="/manage-listing-agent/4740/7/Next">
<div class="style_sub_title_two">Activity Report Email Recipients</div>
<div class="style_mail_warpper"/>
<div id="">
<form id="rmls-activity-report-receipient-form" method="post" accept-charset="UTF-8" action="/manage-listing-agent/4740/7/Next">
<div>
<div id="receipient_add-wrapper" class="form-item">
<input id="activity_listing_id" type="hidden" value="4740" name="activity_listing_id"/>
<div class="rmls_activity_report_receipient_overview_div">
<div class="receipient_submit_div">
<input id="send-submit" class="form-submit style_activity_recipient_sent bluebutton" type="submit" value="Send Report Now" name="op"/>
<input id="submit" class="form-submit style_activity_recipient bluebutton" type="submit" value="Save" name="op"/>
</div>
</div>
</form>
</div>
<div class="activity_report">Top 10 Referrers:</div>
<table class="referrer_table">
</div>
</div>
</form>
<div id="dialog-manage-listing"/>
</div>
<!-- content main -->
<!-- right side bar -->
<!-- end right side bar -->
</div>
<!-- style_content_main_wrapper -->
</div>
<!-- center -->
<div class="clear"/>
<div id="footer">
</div>
<!--end page -->
</div>
<!--end page_wrapper -->
<div id="dialog"/>
<script type="text/javascript"> <!--//--><![CDATA[//><!-- var _gaq = _gaq || [];_gaq.push(["_setAccount", "UA-37131766-1"]);_gaq.push(["_trackPageview"]);(function() {var ga = document.createElement("script");ga.type = "text/javascript";ga.async = true;ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(ga, s);})(); //--><!]]> </script>
<script type="text/javascript"> <!--//--><![CDATA[//><!-- jQuery.extend(Drupal.settings, { "CToolsAJAX": { "scripts": { "/sites/all/modules/jquery_update/replace/jquery.min.js": true, "/misc/drupal.js": true, "/sites/all/modules/blockui/jquery.blockUI.js": true, "/sites/all/modules/blockui/blockui.js": true, "/sites/all/modules/block_edit/block_edit.js": true, "/sites/all/modules/google_analytics/googleanalytics.js": true, "/sites/all/modules/nice_menus/superfish/js/superfish.js": true, "/sites/all/modules/nice_menus/superfish/js/jquery.bgiframe.min.js": true, "/sites/all/modules/nice_menus/superfish/js/jquery.hoverIntent.minified.js": true, "/sites/all/modules/nice_menus/nice_menus.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.core.min.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.dialog.min.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.draggable.min.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.droppable.min.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.sortable.min.js": true, "/sites/all/modules/rmls/js/rmls.js": true, "/sites/all/modules/rmls/jquery.confirm/jquery.confirm.js": true, "/sites/all/modules/rmls/clippy/jquery.clippy.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.datepicker.min.js": true, "/sites/all/modules/date/date_popup/lib/jquery.timeentry.pack.js": true, "/sites/all/modules/date/date_popup/date_popup.js": true, "/misc/tableheader.js": true, "/sites/all/modules/jquery_update/replace/jquery.form.js": true, "/sites/all/modules/ctools/js/ajax-responder.js": true, "/sites/all/modules/jquery_ui/jquery.ui/ui/minified/ui.resizable.min.js": true, "/sites/all/modules/dialog/dialog.js": true, "/sites/all/modules/rmls_order_verification/custom.js": true, "/sites/all/modules/rmls_order_verification/modal-window.min.js": true }, "css": { "/modules/node/node.css": true, "/modules/system/defaults.css": true, "/modules/system/system.css": true, "/modules/system/system-menus.css": true, "/modules/user/user.css": true, "/sites/all/modules/block_edit/block_edit.css": true, "/sites/all/modules/cck/theme/content-module.css": true, "/sites/all/modules/ckeditor/ckeditor.css": true, "/sites/all/modules/ctools/css/ctools.css": true, "/sites/all/modules/date/date.css": true, "/sites/all/modules/date/date_popup/themes/datepicker.css": true, "/sites/all/modules/date/date_popup/themes/jquery.timeentry.css": true, "/sites/all/modules/filefield/filefield.css": true, "/sites/all/modules/nice_menus/nice_menus.css": true, "/sites/all/modules/nice_menus/nice_menus_default.css": true, "/sites/all/modules/rmls/jquery.confirm/jquery.confirm.css": true, "/sites/all/modules/jquery_ui/jquery.ui/themes/default/ui.all.css": true, "/sites/all/modules/jquery_ui/jquery.ui/themes/default/ui.datepicker.css": true, "/sites/all/modules/ubercart/uc_attribute/uc_attribute.css": true, "/sites/all/modules/ubercart/uc_order/uc_order.css": true, "/sites/all/modules/ubercart/uc_product/uc_product.css": true, "/sites/all/modules/ubercart/uc_store/uc_store.css": true, "/sites/all/modules/cck/modules/fieldgroup/fieldgroup.css": true, "/sites/all/modules/views/css/views.css": true, "/sites/all/themes/rmls_webtheme/rmls.css": true, "/sites/all/themes/rmls_webtheme/style.css": true, "/sites/all/themes/rmls_webtheme/grid.css": true, "/sites/all/themes/rmls_webtheme/order_verification.css": true, "/sites/all/modules/rmls_order_verification/modal-window.css": true } } }); //--><!]]> </script>
</div>
<!--end container -->
</div>
<!--end wrapper -->
<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","licenseKey":"61afc6e03f","applicationID":"10285903","transactionName":"ZwQAMUZYW0QAAkZaDF5OIwZAUFpZThNfXxBvDAMLVV5QaAAGV10Xbw0LFkBQW1A+B11BDm8WEARESVBF","queueTime":0,"applicationTime":8543,"atts":"S0MDRw5CSEo=","errorBeacon":"bam.nr-data.net","agent":""}</script>
</div>
</body>
</html>
</document>

3 个答案:

答案 0 :(得分:1)

您还可以使用以下cssSelectors找到按钮:

driver.findElement(By.cssSelector(".placement_save #submit"));
driver.findElement(By.cssSelector(".placement_submit_div #submit"));
driver.findElement(By.cssSelector("#edit-placement-listing-id #submit"));

如果你正在为css和xpath选择器寻找一个非常好的指南,我强烈推荐你this article

答案 1 :(得分:0)

嗨,这有点棘手,但请按照下面的说法进行操作 在我回答之前请不要为什么在第一种情况下它适合你

 driver.findElement(By.xpath("(//input[@name='submit'])[2]")).click();
// cause in this case you were passing valid index value

但是当你传递索引3时,它没有做任何事情因为根据你的问题描述,你的网页上只有3个具有相同属性值的按钮所以在java中,索引开始从 ZERO开始ONE 如此有效的索引值是0,1和2而不是3,因此它不适合你,现在下面就是答案

// first take each button inside the list 
List<WebElement> myButton = driver.findElements(By.name("submit"));
System.out.println("Size of the button the webpage is : " + myButton.size());
// now you can click button on the basis of index like below

myButton.get(0).click(); // for the first button
//myButton.get(1).click(); // for the second button
//myButton.get(2).click(); // for the third button

另外,通过查看您的源代码,我可以识别具有唯一xpath的按钮以及plz,如下所示

// first button
driver.findElement(By.xpath("//*[@class='placement_save']/input")).click();
// second button 
driver.findElement(By.xpath("//*[@class='placement_submit_div']/input")).click(); 

希望这会有所帮助

答案 2 :(得分:0)

感谢大家的建议。 我试过这样,它对我有用: 对于第一个按钮,我这样给了: driver.findElement(By.xpath( “(//输入[@名称= '提交'])[2]”))上单击();

对于第二个按钮,我这样给了: driver.findElement(By.xpath(“.//* [contains(@ class,'form-submit style_activity_recipient bluebutton')]”))。click();