3

I write XPath in Console to find icon element $x("//mat-icon"). Then I get list of elements:

0: <mat-icon class="mat-icon notranslate mat…-color ng-star-inserted" _ngcontent-cvb-c17="" aria-hidden="false" aria-label="start" role="img">
1: <mat-icon class="mat-icon notranslate mat…-color ng-star-inserted" _ngcontent-cvb-c17="" aria-hidden="false" aria-label="start" role="img"> 

and I want to pick the first one so I write index $x("//mat-icon[1]") but it doesn't work. However, if I write $x("//mat-icon")[1] it works, but when I write it in IDE error message appears:

driver.findElement(By.xpath("//mat-icon")[0]).click();
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    You don't need to specify an index. `.findElement()` always returns on the first element found. – JeffC Mar 08 '20 at 22:37
  • 1
    @JeffC: True, but there's an underlying XPath gotcha in play here too: OP should use `(//mat-icon)[1]` to select the first `mat-icon` in the document. `//mat-icon[1]` means *select the `mat-icon` elements that are the first child of its parent*, which may well be different than the first `mat-icon` in the document. See duplicate link for details. – kjhughes Mar 09 '20 at 02:46
  • There are some situations where issue is not with locators, but element is hidden(not interact-able), can you please look at the css style values of current node as well as parent nodes. You may find the reason why you cant interact with it. You may have to use trick of updating css value to interact with element. – Sumit2500 Mar 09 '20 at 11:53
  • @kjhughes Agreed but... OP said `I want to pick the first one` then tried two different ways. 1. `$x("//mat-icon[1]")` which didn't work so it's not the first child of its parent (as you explained). He then tried 2. `$x("//mat-icon")[1]` which just means get the first in the document and it did work. So, in the case presented in this question `.findElement()` would work just fine... no index, nothing complicated needed. – JeffC Mar 09 '20 at 13:16

2 Answers2

2

What about the following:

driver.FindElements(By.xpath("//mat-icon")).get(0).click();

I hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • 1
    This is one way to achieve the result (+1). Note, though, that the basis for OP's misunderstanding is the common mistake regarding the difference between `//mat-icon[1]` and `(//mat-icon)[1]`. See duplicate link for further details. – kjhughes Mar 09 '20 at 02:42
  • @kjhughes, thanks for the clarification. – Ratmir Asanov Mar 09 '20 at 06:26
  • `driver.findElements(By.xpath("//mat-icon"))[0].click()` does not work. There is error "Array type expected" –  Mar 09 '20 at 08:49
  • @Yamis, I have updated my answer. Now it should work. I forgot that it's Java :) – Ratmir Asanov Mar 09 '20 at 18:30
2

To pick the first <<mat-icon> element you can use either of the following Locator Strategies:

  • Using findElements(), and get():

    driver.findElements(By.cssSelector("mat-icon")).get(0).click();
    
  • Using findElements(), and get():

    driver.findElements(By.xpath("//mat-icon")).get(0).click();
    
  • Using findElements(), and index:

    driver.findElements(By.xpath("//mat-icon")).get(0).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    I see no reason for a downvote here; I'll counter it with an up. Note, though, that the basis for OP's misunderstanding is the common mistake regarding the difference between `//mat-icon[1]` and `(//mat-icon)[1]`. See duplicate link for further details. – kjhughes Mar 09 '20 at 02:40
  • @DebanjanB `driver.findElements(By.xpath("//mat-icon"))[0].click()` does't work. There is error "Array type expected" –  Mar 09 '20 at 08:51
  • @Yamis How about the first option? – undetected Selenium Mar 09 '20 at 09:26
  • @DebanjanB First option work, but i don't use cssSelectors to find elements. I would say that i don't know cssSelector commands:) –  Mar 09 '20 at 09:32
  • @Yamis Checkout the updated answer and let me know the status. – undetected Selenium Mar 09 '20 at 10:49