Home > Software engineering >  How to find element of the below HTML?
How to find element of the below HTML?

Time:10-15

Am curious if anyone might know which Selenium locating element method would be used to identify the below html.

I am trying to locate and 'click'

button type="submit" tabindex="3" data-ng-click="login()" class="btn btn-default" data-ng-disabled="loginForm.$invalid" data-ng-class="{ 'gray': loginForm.$invalid }">Login</button

CodePudding user response:

It depends, if you know text is not gonna change, direct use text

//button[text()='Login']

or based on attributes

//button[@data-ng-click='login()']

You can combine these two like below :

//button[@data-ng-click='login()' and text()='Login']

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

CodePudding user response:

You can following xpath,

  1. //button[@type='submit']
  2. //button[@data-ng-click='login()']
  3. //button[text()='login']

driver.findElement(By.xpath("//button[text()='X']")).click();

  • Related