'JS Script to get attribute value using JavaScriptExecutor and selenium

Can somebody help me with a js script that would get an value for an attribute? I would prefer to use as locator an xPath

HTML Element:

<td class="WarningMessage_row-checked-width__2Z5wh"><label id="label-Capitalised-0" data-testid="Capitalised-0" class="checkBox_checkbox-container__1S363 checkBox_checked__1_BlG"> <input aria-checked="true" type="checkbox" index="0" name="Capitalised" id="Capitalised-0" class="checkbox-0" data-testid="Capitalised-0-checkBox" value="" checked=""><span class="checkBox_checkmark__1VFwk"></span></label></td>

Xpath:

'//*[@name='Capitalised']/ancestor::label/input'

Attribute name:

aria-checked

Basically i want to get the attribute value of element: input, attr aria-checked and the execute it using JavaScriptExecutor with selenium webdriver:

JavascriptExecutor js = (JavascriptExecutor) driver;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source