0

Can anyone help, I'm trying to get the last xpath element for the button that represents the smart card (e.g. 633597015500042010) which is dynamically generated, then to click on it. I've tried the following but still can't find a solution. The name of the button I'm intending to capture is "Select".

driver.findElement(By.xpath("//div[@class='select-item']/div)[last()]")).click();

<html class="js flexbox flexboxlegacy canvas canvastext postmessage no-websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients no-cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache desktop" lang="en">
<head>
<body class="rwd">
<noscript> <iframe src="//www.googletagmanager.com/ns.html?id=GTM-KZFHXV" height="0" width="0" style="display:none;visibility:hidden"></iframe> </noscript>
<script>
<div class="page-wrapper page-wrapper-delivery">
<div>
<div class="wrapper-wide wrapper-header" style="position: relative;">
<div class="wrapper-wide wrapper-body">
<div class="wrapper">
<div class="main">
<a id="skipnav" tabindex="-1"></a>
<div class="row hidden">
<div class="row">
<div class="row">
<div class="cf"></div>
<div class="row">
<div class="pane base12">
<form class="cf javascript-validate" novalidate="novalidate">
<div class="component checkout-page cf">
<div class="component ticket-item-base ticket-item-checkout cf">
<div class="cf">
<div class="component smart-card-checkout cf">
<div class="component vertical-accordion stag-standard-accordion checkout-accordion parent-accordion cf">
<ul id="accordion-1" class="accordion single-display">
<li class="cf open-li">
<a class="toggle-link open" href="#"> Select/request a smart card </a>
<a class="edit-link secondary-toggle-link" href="#">Edit</a>
<div class="accordion-drop" style="display: block;">
<div class="accordion-content cf">
<div class="component vertical-accordion stag-standard-accordion child-accordion cf">
<ul id="accordion-2" class="accordion multi-display">
<li class="cf open-li">
<a class="toggle-link open" href="#">Select a smart card</a>
<div class="accordion-drop" style="display: block;">
<div class="accordion-content cf">
<div class="info-box">
<form class="select-card-form" novalidate="" method="post">
<input type="hidden" value="SmartCardSelect_6822d668-9a46-4b51-910d-98c701d43fa2" name="FormName">
<input type="hidden" value="e3429d28-e838-4247-9a4e-0b3dc53cf1a7" name="CardId">
<div class="select-item ">
<p>Smart card 1</p>
<button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500041558">
</div>
</form>
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
<input type="hidden" value="SmartCardSelect_6822d668-9a46-4b51-910d-98c701d43fa2" name="FormName">
<input type="hidden" value="94888c8a-815c-484e-808c-8d400694b9e9" name="CardId">
<div class="select-item ">
<p>Smart card 7</p>
<button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500042010">
<span>Select</span>
</button>
</div>
</form>
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
<form class="select-card-form" novalidate="" method="post">
</div>
</div>
</li>
<li class="cf">
<li class="cf">
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
<div class="component checkout-date-select cf no-end-date">
</div>
</div>
</div>
</div>
<div class="row">
<div class="row">
</div>
</div>
</div>`enter code here`
AzMar
  • 71
  • 2
  • 8
  • Anyone around?..-:) – AzMar Jun 13 '16 at 15:09
  • 1
    A html indent would help. The html does not seems to be complete and also you don't have to post all the html, only the main section with the element you need.Try to identify a unique parent for your element and also try this: //span[text()='Select']/parent::button – lauda Jun 14 '16 at 22:21

1 Answers1

0

As per provided HTML code, required element is inside the frame, so we need to switch into that frame. After switching into frame, look for that button

In code, xpath used is not correct. It should be like this

 (//div[@class='select-item']/button)[last()]

considering, your are trying find the location of following element

 <button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500042010">
murali selenium
  • 3,847
  • 2
  • 11
  • 20
  • I've tried to switch to frame as follows but still can't get it to work - any idea why? : driver.switchTo().frame("submit-btn uniform-button button-smaller button-orange select-address"); driver.findElement(By.xpath("//div[@class='select-item']/button)[last]")).click(); – AzMar Jun 13 '16 at 19:45
  • No..that is wrong. Frame means in HTML it starts with – murali selenium Jun 14 '16 at 04:07
  • or anyone - please check this code as I'm still not getting a solution to this: driver.switchTo().defaultContent(); driver.switchTo().frame("//iframe[@src='//www.googletagmanager.com/ns.html?id=GTM-KZFHXV']"); driver.findElement(By.xpath("//div[@class='select-item']/button)[last()]")).click(); – AzMar Jun 14 '16 at 08:45