如何按文本查找元素并单击另一个div

时间:2017-06-28 07:24:05

标签: selenium-webdriver

我正在尝试自动化一个网站,在该网站中我获得了大量记录,并且我想找到包含特定文本的记录,然后单击与该记录对应的按钮。我面临的问题是我可以找到文本,但按钮在另一个div中。有人可以帮忙吗? 下面是html:



<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="col-lg-5 col-md-5 col-sm-5">
<p class="f-12 ng-binding">
<!-- ngIf: item.childCategoryName.length>0 -->
<p class="f-13 black-text ng-binding ng-scope" ng-if="item.childCategoryName.length>0">
<!-- end ngIf: item.childCategoryName.length>0 -->
<p class="f-13 black-text">
<p class="f-13 black-text">
<h3 id="content" class="s-job ng-binding">test47</h3>
<p id="content" class="f-12 m-t-20 ng-binding">test47 </p>
</div>
<div class="col-sm-4 col-md-4 col-lg-4 ">
<div class="pull-right">
<p class="">
<button id="121" class="btn btn-green" ng-click="jobInfo(item.taskId)">
<span class="pull-left ng-binding">Voir la tâche</span>
<span class="circle"/>
</button>
</div>
</div>
</div>
<hr/>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

请尝试以下操作,让我知道结果:

driver.findElement(By.xpath("//h3[.='" + title + "']/following::button[.='Voir la tâche']")).click()

您还可以在DOM中添加一些显式等待元素:

WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[.='" + title + "']/following::button[.='Voir la tâche']"))).click();

或检查您的button located inside an iframe