[英]Count number of elements using xpath() in javascript on selenium webdriver
I am writing test script in selenium webdriver using javascript.我正在使用 javascript 在 selenium webdriver 中编写测试脚本。
I am using mocha and phantomJS too.我也在使用 mocha 和 phantomJS。
I want to count total no.我要数总数。 of
<li>
in a <ul>
having a particular id. <li>
在具有特定 id 的<ul>
中。
I am using this script:我正在使用这个脚本:
var majorSize = driver.findElements(By.xpath("//ul[contains(@id, 'side-menu')]/li[not(contains(@class, 'nav-header'))]")).size();
console.log(majorSize);
I used the reference from this我从使用的参考此
But I am getting error:但我收到错误:
TypeError: undefined is not a function
In javascript, I can use this code also:在 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);
The above script has majorLink scope withing the then() function only.上面的脚本只有 then() 函数的主要链接范围。 So it can't be used outside.
所以不能在外面使用。 But I need it to be used outside as well.
但我也需要它在外面使用。
If I define something like var majorSize
, then also it just displays 0 outside the then() function.如果我定义了类似
var majorSize
东西,那么它也只会在 then() 函数之外显示 0。
What wrong am I doing?我做错了什么?
Here is the HTML code:这是 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>
If I understand correctly, you want all the LI
s (and only the LI
s) that are children of the UL
with id = side-menu
.如果我理解正确,您需要所有
LI
(并且只有LI
)都是id = side-menu
的UL
孩子。 Is that correct?那是对的吗? This is in Java but hopefully you can translate it.
这是在 Java 中,但希望你能翻译它。
List<WebElement> lis = driver.findElements(By.cssSelector("#side-menu > li"));
System.out.println(lis.size());
This CSS selector means find an element with an ID (#) of side-menu that has children LI
s.这个 CSS 选择器意味着找到一个带有 ID (#) 的 side-menu 的元素,它有子元素
LI
。 These are direct children and not descendants.这些是直系子女,而不是后代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.