0

I am trying to automate a site in which I get numbers of records and I want to find a record which contains a particular text and then click on the button corresponding to that record. Problem I am facing is that I can find the text but the button is in another div. Can anyone help? Below is the 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>
Mansi
  • 63
  • 3
  • 11
  • Can you share your current code? – Andersson Jun 28 '17 at 07:26
  • I have tried driver.findElement(By.xpath("//button[@class= 'btn btn-green'] and [contains(text(), '" + title + "')]")).click(); and driver.findElement(By.xpath("//*[contains(text(), '" + title + "')]/../../../div[@class= 'col-sm-4 col-md-4 col-lg-4']/div/button[@class= 'btn btn-green']")).click(); – Mansi Jun 28 '17 at 07:43

1 Answers1

0

Try below and let me know the result:

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

You can also add some explicit wait for element to present in DOM:

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

or check if your button located inside an iframe

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thanks for the reply. But it didn't work. I got: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(text(), 'SELENIUM TEST 13')]/../../div[@class= 'col-sm-4 col-md-4 col-lg-4']/div/button[@class= 'btn btn-green']"} – Mansi Jun 28 '17 at 08:46
  • sorry I posted wrong traceback: The correct one is no such element: Unable to locate element: {"method":"xpath","selector":"//h3[.='SELENIUM TEST 16']/following::button[.='Voir la tâche']"} – Mansi Jun 28 '17 at 09:20
  • I tried the explicit wait, but it didn't worked. and also "button" is not located inside iframe – Mansi Jun 28 '17 at 10:25