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

The above one is html for an element.

String ok= TickBox.get(3).getAttribute("aria-hidden");
System.out.println(ok);
Assert.assertTrue(TickBox.get(3).getAttribute("aria-hidden") != null);

I am using above code to verify, but it returning null.

Ash
  • 69
  • 4
  • 12
  • What is `TickBox`? how do you locate the element? – Guy Oct 09 '18 at 13:19
  • @FindBy(xpath=".//*[@class='width-90px text-ellipsis align-center margin-top-bottom-5']") private List TickBox @Guy – Ash Oct 09 '18 at 13:28
  • Possible duplicate of [Difference b/w getText() and getAttribute() in Selenium WebDriver?](https://stackoverflow.com/questions/32307702/difference-b-w-gettext-and-getattribute-in-selenium-webdriver) – JeffC Oct 09 '18 at 13:31
  • With that locator, you are getting the `H5` element and that element doesn't have the `aria-hidden` attribute, the `I` tag does. Create a locator that finds the `I` tag and then you'll be able to get the attribute. – JeffC Oct 09 '18 at 13:33
  • h5 class also belongs to same element here, why it is not returning aria-hidden value? @JeffC – Ash Oct 09 '18 at 13:45
  • The `H5` element and the `I` element are two different elements. There is no "h5 class" so I'm not sure what you are referring to. – JeffC Oct 09 '18 at 16:07

2 Answers2

2

To retrieve the value of the attribute aria-hidden you can use either of the following solutions:

  • Using cssSelector:

    System.out.println(driver.findElement(By.cssSelector("h5.text-ellipsis>i.fa.fa-check.font-14")).getAttribute("aria-hidden"));
    
  • Using xpath:

    System.out.println(driver.findElement(By.xpath("//h5[contains(@class,'text-ellipsis')]/i[@class='fa fa-check font-14']")).getAttribute("aria-hidden"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i have to use @FindBy(xpath=".//*[@class='width-90px text-ellipsis align-center margin-top-bottom-5']") private List TickBox; i am checking a table where few boxes are having tick i.e aria-hidden is true for that, so this xpath is common for all boxes. – Ash Oct 09 '18 at 13:25
  • My question is same getAttribute() is not returning aria-hidden value, its returning null. – Ash Oct 09 '18 at 13:34
  • @Ash That is because your xpath is too loose and isn't returning the element that you expect. A simple way to do the xpath would be by index. (//i[contains(@class, 'fa-check')])[1] – Michiel Bugher Oct 09 '18 at 16:04
0

Try with jquery execution from selenium driver.

Example jquery: $('h5 >i.fa.fa-check').getAttribute('aria-hidden');

Get the result of attribute

String attributeValue = ((JavascriptExecutor) driver).executeScript("return $('h5 >i.fa.fa-check').getAttribute('aria-hidden');");
Infern0
  • 2,565
  • 1
  • 8
  • 21