java中的selenium无法点击href =“javascript:void(0);”

时间:2018-06-18 20:22:31

标签: javascript java selenium-webdriver bootstrap-accordion

这是我正在处理的一段代码。我正在尝试点击子菜单。

<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>

我正在尝试点击帐户组和帐户信息中心。他们中的任何一个都在接收点击。这个代码已经失败了。

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;
}

1 个答案:

答案 0 :(得分:1)

  1. 点击Accounts Dashboard

    Accounts Dashboard的href中的

    javascript:void(0);返回undefined。当试图点击这样的链接时,执行单击并且没有获得方法返回值。这给人的印象是不执行单击。使用

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

    而不是submenu.click();

  2. 点击“帐户组”

    Selenium点击此链接并将href #one1 附加到确认点击已执行的网址。