0

Below is my code to locate an element.

WebElement clickNextButton = driver.findElement(By.xpath("//button[contains(text(),'Export')]"));
         clickNextButton.click();

I want to locate a button,among multiple buttons with same name and with same CSS code as below:

<button ng-disabled="tx.QA_Status != 'VERIFIED'" class="btn btn-primary gray btn-xs" data-toggle="modal" data-target="#exportTransactionModal" ng-click="resetModal('exportTransactionModal');exportTransaction(tx.head,tx.transactionId);">
                                <i class="fa fa-file-text-o"></i>&nbsp;Export
                            </button>


<button ng-disabled="tx.QA_Status != 'VERIFIED'" class="btn btn-primary gray btn-xs" data-toggle="modal" data-target="#exportTransactionModal" ng-click="resetModal('exportTransactionModal');exportTransaction(tx.head,tx.transactionId);">
                                <i class="fa fa-file-text-o"></i>&nbsp;Export
                            </button>                           

I am unable to locate because transaction is done using AngularJS. and exportTransaction method is called in JS file.

0
By.xpath("(//button[@class='btn btn-primary gray btn-xs'])[1]");

The above xpath will locate the first match.

  • when i search for xpath it shows "/html/body/div[2]/div[7]/table/tbody/tr[1]/td[5]/button[1]" – PriR May 7 '18 at 7:33
  • You have used absolute xpath.Instead of using absolute xpath ,start using relative xpath because when there is a change in the DOM ,absolute xpath will not work. – shanila May 7 '18 at 8:23
0

Why not

$$('.btn.btn-primary.gray.btn-xs').first();

if the Export button is first element

Or

$$('.btn.btn-primary.gray.btn-xs')[0] //for the first element with the class.

Or

$$('.btn.btn-primary.gray.btn-xs').get(index).element(by.xpath("//button[contains(text(),'Export')]"))
  • /* WebElement container = button.findElement(By.xpath("current()/ancestor-or-self::button[1][contains(@class, 'btn.btn-primary.gray.btn-xs')]") ); container.click();*/ /*driver.manage().window().maximize(); WebDriverWait wait5 = new WebDriverWait(driver, 30); WebElement button3 = wait5.until(ExpectedConditions.elementToBeClickable(By.cssSelector("btn.btn-primary.gray.btn-xs[text()='Export'][0]"))); button3.click();*/ – PriR May 7 '18 at 7:21
  • i iused all above formats. – PriR May 7 '18 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.