Search code examples
javaxpathselenium-webdriverbuttonclick

How to uniquely identify buttons using Xpath if the elements like 'id', 'class', 'type', 'value' and 'name' are same for all the buttons


I have three buttons in my page out of which I need to click two buttons at different points of time. But I'm not able to do this since the xpath and other html elements (id, class, type, value and name) are same for all these buttons. How can I uniquely identify these buttons for clicking? Note that the buttons are at different location in the same page, and they need to be clicked one after the other.

I have tried like this:

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

and was able to click one button. But when I tried with '3' instead of two, nothing happened. 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>

Solution

  • Hi this is little bit tricky but please do it like below it will work before my answer please not why in first case it worked for you

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

    but when you pass index 3 it does nothing cause as per your question description you have only 3 buttons on your webpage with same attribute values so in java the indexing starts form ZERO not form ONE so valid index values are 0,1 and 2 only not 3,hence it not worked for you ,now below is the answer

    // 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
    

    Also by looking at your source code i can identify the buttons with unique xpath as well plz do it like below

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

    hope this helps