2
<td class="width-90px">
    <h5 class="width-90px text-ellipsis align-center margin-top-bottom-5">
            <i class="fa fa-check font-14 " aria-hidden="true"></i>
    </h5>
</td>

In above html class="width-90px" is one element, under this element another element class="fa fa-check font-14 " is present, how can we give condition to check second element is present inside first element ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ash
  • 69
  • 4
  • 12

5 Answers5

2

You can use XPath with appropriate predicate:

//td[@class="width-90px" and .//i[@class="fa fa-check font-14 "]]
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • 1
    xpath has its pros and cons: pros less call for a driver find logic; cons it will fail in both cases and you will have to double check which happened 1) the first element is not present 2) the second element is nor present – Vladimir Efimov Oct 26 '18 at 09:36
  • @VladimirEfimov , 1) If ancestor is not present, there is no need to check descendant, 2) If there is no descendant, then condition failed. Anyway, the question is too broad, it has no code attempts and so exact goal and problem is not quite clear – Andersson Oct 26 '18 at 09:44
1
WebElement first = driver.findElement(By.xpath(".//*[@class='width-90px']"));

first.findElement(By.xpath(".//*[@class='fa fa-check font-14 ']")).isDisplayed();

is working fine, if second element is not present inside first element its throwing error.

Ash
  • 69
  • 4
  • 12
  • @Ash , also note that your answer doesn't really suite to your question. According to docs `isDisplayed()` should check *"Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute."* while you want *"...to check second element is present inside first element "*. Element can be inside another element, but be hidden, so your approach will fail – Andersson Oct 26 '18 at 09:49
1

If your sole aim is to validate that the element with attribute as class="fa fa-check font-14 " is present under the element with attribute as class="width-90px" you can construct a Locator Strategy based on a simple Ancestor and Descendant relation and can use either of the following solutions:

  • XPath:

    //td[@class="width-90px"]//i[@class='fa fa-check font-14']
      ^ -> Ancestor            ^ -> Descendant
    
  • CssSelector:

    td.width-90px i.fa.fa-check.font-14
    ^ ->Ancestor  ^ ->Descendant
    

Note: Use a try-catch{} block to validate the presence of the element based on Ancestor-Descendant relationship.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You have to first find element with class "width-90px" and inside that you can check element with class "fa fa-check font-14 " as below:

WebElement first = driver.findElement(By.className("width-90px"));

WebElement second  = first.findElement(By.className("fa fa-check font-14 "));

2nd statement in code will throw exception if it is not present inside first element.

  • second.isDisplayed(); will not validate that the second element is present inside the first element, it will just check whether element is present on the page. @Mitul Lakhani – Ash Oct 26 '18 at 06:29
  • @Ash Yes, I mentioned that in last statement in my answer it will check element is displayed or not. If element is not present 2nd line of code with throw exception. So this code will be useful – Mitul Lakhani Oct 26 '18 at 06:31
  • Hey @MitulLakhani Your answer is more likely to be new version of Andersson's answer with no optimization and no improvement. – Ashish Kamble Oct 26 '18 at 07:16
  • 1
    @MitulLakhani , note that you cannot pass compound class name to `By.className`, so your code will lead to Exception – Andersson Oct 26 '18 at 07:25
  • 1
    @Andersson any locators(which ever is fine for tester) can be used instead of class name, but same approach will work – Mitul Lakhani Oct 26 '18 at 08:37
  • @AshishKamble It is not a new version of a same answer..It is different approach to achieve same thing – Mitul Lakhani Oct 26 '18 at 08:38
  • 1
    @MitulLakhani , yeah, any can be used, but you've selected the one that is not working – Andersson Oct 26 '18 at 08:44
  • @MitulLakhani different approach which will results into exception. Do you really understood what By.className(" ") is and [@class=" "] is ??? Whatever please modify your answer to contribute here is my kind request. – Ashish Kamble Oct 26 '18 at 08:53
  • @MitulLakhani , `isDisplayed` should check *if element is displayed*, but not *if element is present*, so anyway your approach is not correct – Andersson Oct 26 '18 at 09:52
0

You can achieve this using Xpath Axes:

WebElement element = driver.findElement.(By.xpath("//td[@class='width- 
                     90px']/following-sibling::h5/i[@class='fa fa-check font-14']));
if(element != null)
   //element is inside first 
else
   //element is not the child of first
preeth
  • 26
  • 3