Search code examples
javascriptc#seleniumhidden

Selenium how to click a button with hidden attribute


I need to click on a button that is part of a grid menu. It show's the item price column in the grid, by default it is hidden.

<button type="button" class="ui-grid-menu-item" ng-click="itemAction($event, title)" ng-show="itemShown()" ng-class="{ 'ui-grid-menu-item-active': active(), 'ui-grid-sr-only': (!focus &amp;&amp; screenReaderOnly) }" aria-pressed="" tabindex="0" ng-focus="focus=true" ng-blur="focus=false" aria-hidden="true"><i ng-class="icon" aria-hidden="true" class="ui-grid-icon-ok">&nbsp;</i> Item Price</button>

I took an approach to first making it shown by setting aria-hidden="false".

        IJavaScriptExecutor js = (IJavaScriptExecutor)(_chromeDriver);
        js.ExecuteScript("document.getElementByXPath('//*[@id='menuitem-5']/button').setAttribute('aria-hidden', 'false')");

But I'm getting javascript error.

javascript error: missing ) after argument list
  (Session info: chrome=81.0.4044.138)

I've checked this answer also

Selenium EventFiringWebDriver JavaScript: SyntaxError: missing ) after argument list

But no solution yet. How do I resolve it? Is my approach right or should I change.?


Solution

  • You can use javascript to click the element directly

    IWebElement element = _chromeDriver.FindElement(By.XPath("//*[@id='menuitem-5']/button"));
    
    js.ExecuteScript("arguments[0].click();", element);
    

    I think the error you are seeing is from this line:

    IJavaScriptExecutor js = (IJavaScriptExecutor)(_chromeDriver);
    

    change it to this:

    IJavaScriptExecutor js = ((IJavaScriptExecutor)_chromeDriver);