在selenium webdriver上使用javascript中的xpath()计算元素数量

时间:2015-08-31 10:22:48

标签: javascript xpath selenium-webdriver

我正在使用javascript在selenium webdriver中编写测试脚本。

我也在使用mocha和phantomJS。

我想算总数没有。具有特定身份的<li><ul>的{​​{1}}。

我正在使用这个脚本:

var majorSize = driver.findElements(By.xpath("//ul[contains(@id, 'side-menu')]/li[not(contains(@class, 'nav-header'))]")).size();
console.log(majorSize);

我使用了来自this

的引用

但我收到错误:

TypeError: undefined is not a function

在javascript中,我也可以使用此代码:

driver.findElements(By.xpath("//ul[contains(@id, 'side-menu')]/li[not(contains(@class, 'nav-header'))]")).then(function(majorSize)
{
   console.log(majorSize.legth)
});
console.log(majorSize.legnth);

上述脚本的majorLink范围仅包含then()函数。所以它不能在外面使用。但我也需要它在外面使用。

如果我定义var majorSize之类的东西,那么它也只是在then()函数之外显示0。

我做错了什么?

以下是HTML代码:

<ul id="side-menu" class="nav">
    <li class="nav-header">
    <img class="logo" alt="Track Revenue" src="/images/3c4939d.png">
    <div class="logo-element"> TR </div>
    </li>
    <li class="">
    <a href="#home">
    <i class="fa fa-bolt"></i>
    <span class="nav-label">Tr Admin Menu</span>
    <span class="fa arrow"></span>
    </a>
    <ul class="nav nav-second-level collapse" aria-expanded="false" style="height: 0px;">
        <li>
        <a href="/admin/user/">All Users</a>
        </li>
        <li>
        <a href="/admin/company/">All Companies</a>
        </li>
        <li>
        <a href="/admin/device/">Devices</a>
        </li>
        <li>
        <a href="/admin/email/">Send Email</a>
        </li>
        <li>
        <a href="/admin/impersonate">Impersonate User</a>
        </li>
        <li>
        <a href="/admin/encrypttest">Test Encryption</a>
        </li>
    </ul>
    </li>
    <li class="">
    <a href="#home">
    <i class="fa fa-th-large"></i>
    <span class="nav-label">Campaigns</span>
    <span class="fa arrow"></span>
    </a>
    <ul class="nav nav-second-level collapse" aria-expanded="false" style="height: 0px;">
    <li>
    <a href="http://demotest.com.co/main/account/campaign_overview.php">Overview</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/campaign_update_cpc.php">CPC Update</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/campaign_update_subids.php">SubID Update</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/campaign_v2.php">Add Campaign</a>
        </li>
    </ul>
    </li>
    <li>
    <a href="#home">
    <i class="fa fa-bar-chart-o"></i>
    <span class="nav-label">Stats</span>
    <span class="fa arrow"></span>
    </a>
    <ul class="nav nav-second-level collapse">
        <li>
        <a href="http://demotest.com.co/main/account/stats_campaign_v2.php">Campaign Stats</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/stats_week_day_v2.php">Week / Day Parting Stats</a>
        </li>
    </ul>
    </li>
    <li>
    <a href="#home">
    <i class="fa fa-files-o"></i>
    <span class="nav-label">Reports</span>
    <span class="fa arrow"></span>
    </a>
    <ul class="nav nav-second-level collapse">
        <li>
        <a href="http://demotest.com.co/main/account/report_custom.php">Custom Data Reports</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/report_subid.php">SubID Analysis Report</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/completed_report.php">Scheduled Reports</a>
        </li>
    </ul>
    </li>
    <li>
    <a href="#home">
    <i class="fa fa-cog"></i>
    <span class="nav-label">Settings</span>
    <span class="fa arrow"></span>
    </a>
    <ul class="nav nav-second-level collapse">
        <li>
        <a href="/profile/">Account</a>
        </li>
        <li>
        <a href="/plan/">Plan Management</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/settings_groups.php">Campaign Groups</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/settings_network.php">Affiliate Networks</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/settings_sources.php">Traffic Source</a>
        </li>
        <li>
        <a href="/manage/user/">Manage Users</a>
        </li>
        <li>
        <a href="/manage/company/">Manage Company</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/settings_rules.php">Blocking & Filter Rules</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/domains.php">Domains</a>
        </li>
        <li>
        <a href="http://demotest.com.co/main/account/campaign_archive.php">Campaign Maintenance</a>
        </li>
    </ul>
    </li>
</ul>

1 个答案:

答案 0 :(得分:-2)

如果我理解正确,您希望LI的所有LI(以及UL s}都是id = side-menu List<WebElement> lis = driver.findElements(By.cssSelector("#side-menu > li")); System.out.println(lis.size()); 。那是对的吗?这是Java版,但希望你能翻译它。

LI

这个CSS选择器意味着找到一个带有子out <- matrix(0L, nrow = nrow(x), ncol = max(x)) for (i in 1:nrow(x)) out[i, x[i, ]] <- 1L > out [,1] [,2] [,3] [,4] [1,] 1 1 0 0 [2,] 1 0 1 0 [3,] 1 0 0 1 [4,] 0 1 1 0 [5,] 0 1 0 1 [6,] 0 0 1 1 的侧边菜单ID(#)的元素。这些是直接的孩子,而不是后代。