Search code examples
javascriptjavaselenium-webdriverbootstrap-accordion

selenium in java cannot click on a href="javascript:void(0);"


This is the piece of code which I am working on.I am trying to click on sub menu.

<ul class="accordion">
     <li id="one1" class="files">
       <a href="#one1">Accounts</a>
       <ul class="sub-menu">
       <li>

               <a href="#one1" onClick="loadTabv2('accounts/LoadAccountGroupMaster',
                  'Account Group','accounts_account_group');"><img src="layouts/static/image/micons/account_group.png">Account Group</a>

               <a href="#one1" onClick="loadTabv2('accounts/AccountingPeriod',
                  'Accounting Period','account_accounting_period');"><img src="layouts/static/image/micons/accounting_period.png">Accounting Period</a>

                   <a href="javascript:void(0);" onClick="loadTab('accounts/LoadAccountMaster',
                 'Account Master','account_account_master');"> <img src="layouts/static/image/micons/account_master.png">Account Master</a>

                   <a href="javascript:void(0);" onClick="loadTab('accounts/AccountsDashboard',
                 'Accounts Dashboard','accounts_dashboard');"> <img src="layouts/static/image/micons/accounts_dashboard.png">Accounts Dashboard</a>

                   <a href="javascript:void(0);" onClick="loadTab('accounts/AccountSettings',
                 'Account Settings','accounts_account_settings');"> <img src="layouts/static/image/micons/account_settings.png">Account Settings</a>

I am trying to click on account group and accounts dashboard.Neither of them are receiving clicks.Tried this code and failed.

List<WebElement> submenus=driver.findElements(By.xpath("//li[@id='one1']//li//a"));
 for (WebElement submenu: submenus) {
System.out.println("values of each submodule : 
"+submenu.getAttribute("innerHTML"));
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
if (submenu.getAttribute("innerHTML").contains("Accounts Dashboard")) {
  submenu.click();
  break;
}

Solution

    1. Clicking on Accounts Dashboard

      javascript:void(0); in the href of Accounts Dashboard returns undefined. When tried to click on such a link, click is performed and no method return value is obtained.This gives an impression that click is not performed. Use

      JavascriptExecutor jsexec = (JavascriptExecutor) driver; jsexec.executeScript("arguments[0].click();", submenu);

      instead of submenu.click();

    2. Clicking on Account Group

      Selenium clicks on this link and href #one1 is appended to the URL confirming that click is performed.