从列表中选择最后一个webElement

时间:2018-01-31 10:37:35

标签: java selenium-webdriver

教它可能很简单,但卡住了。有人可以帮我跟进: 需要从列表中拾取最后一个元素(而不是其他其他元素)。找到所有元素:

List<WebElement> all1 = driver.findElements(By.xpath("//main[@class='main row']/article/div/div[2]/div[1]/div/div[1]/ol[1]//a"));

试着用这个来接最后一次:

WebElement lastElement = all.get(all.size() - 1);

运行测试后出现以下错误:

ArrayIndexOutOfBounds -1

我做错了什么?提前谢谢

enter image description here

HTML:

<article>
<p><strong>If you earn yourself a prize/strong></p>
<div class="row">
<div class="row">
 <div style="width:100%; height:0; position: relative; padding-   bottom:56.25%"><iframe src="//site13251226.23video.com/v.ihtml   /player.html?token=4e11b3a23e3bb16ec53f380619c36c93&amp;source=embed&amp; photo%5fid=19283817" style="width:100%; height:100%; position: absolute; top:  0; left: 0;" frameborder="0" border="0" scrolling="no" allowfullscreen="1"  mozallowfullscreen="1" webkitallowfullscreen="1"></iframe></div>
 </div>
 <div class="row">
 <p></p><div class="row">
 <div class="promotion">
 <h2 class="promotion-header competition-title">month <span>Prize:   £3000</span></h2>
 <div class="promotion-body competition">
 <div class="row">
 <div class="large-8 column">
 <div class="t-box-padding">
 <p>How well do you know .</p>
 </div>
 </div>
 <div class="large-4 column trophy padding-reset text-center">
 <img src="">
 </div>
 </div>
 <div class="row">
 <div class="large-12 column">
 <p class="prize">Prize: <strong>£3000</strong></p>
 </div>
 </div>
 <h2>Current standings <small>( 1. Jan 2018 - 1. Feb 2018)</small></h2>
  <ol class="competition-standings">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">
 <li class="list-item row">

1 个答案:

答案 0 :(得分:0)

找到解决方案 - muy错误 - 选择了错误的xpath并且正确的是:

List<WebElement> all = driver.findElements(By.xpath("//main[@class='main row']/article/div/div[1]/div//ol[1]/li/div[@class='column large-6 competition-standings-user ellipsis']/a"));