按钮的最后一个元素

时间:2016-06-13 13:50:28

标签: java selenium xpath selenium-webdriver

任何人都可以提供帮助,我正在尝试获取代表智能卡的按钮的最后一个xpath元素(例如633597015500042010),这是动态生成的,然后点击它。我尝试了以下但仍无法找到解决方案。我打算捕获的按钮的名称是“选择”。

driver.findElement(By.xpath( “// DIV [@类= '选择项'] / DIV)[最后一个()]”))点击();

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

1 个答案:

答案 0 :(得分:0)

根据提供的HTML代码,所需元素在框架内,因此我们需要切换到该框架。切换到框架后,查找该按钮

在代码中,使用的xpath不正确。它应该是这样的

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

考虑到,您正在尝试查找以下元素的位置

 <button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500042010">