Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从标记中获取所有元素_Java_Selenium Webdriver_Testng - Fatal编程技术网

Java 如何从标记中获取所有元素

Java 如何从标记中获取所有元素,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,如何从所有元素中获取所有标题和计数(6) 我需要得到这些值 2000卢比-低于2000卢比 2001卢比-5000卢比 5001卢比-10000卢比 10001卢比-18000卢比 18001卢比-25000卢比 250001卢比-35000卢比 35001卢比及以上 和值6344112594 下面是html <div class="body"> <div class="oneFacet bmargin5"> <input class="expandedConten

如何从所有元素中获取所有标题和计数(6)

我需要得到这些值

2000卢比-低于2000卢比
2001卢比-5000卢比
5001卢比-10000卢比
10001卢比-18000卢比 18001卢比-25000卢比 250001卢比-35000卢比 35001卢比及以上

和值6344112594

下面是html

<div class="body">
<div class="oneFacet bmargin5">
<input class="expandedContent dont-show" type="text">
<div class="loading"></div>
<div class="head line">
<ul id="price_range" class="facets" nofilter="1" displaytype="" keepcollapsed="" valuelimit="">
<li class="facet" title="Rs. 2000 and Below">
<a class=" active ">
<input class="facetoption" type="checkbox" value="facets.price_range%5B%5D=Rs.+2000+and+Below" autocomplete="off">
<span class="title fk-inline-block lmargin5" original="Rs. 2000 and Below">Rs. 2000 and Below</span>
<span class="count">(6)</span>
</a>
</li>
 <li class="facet" title="Rs. 2001 - Rs. 5000">
<a class=" active ">
<input class="facetoption" type="checkbox" value="facets.price_range%5B%5D=Rs.+2001+-+Rs.+5000" autocomplete="off">
<span class="title fk-inline-block lmargin5" original="Rs. 2001 - Rs. 5000">Rs. 2001 - Rs. 5000</span>
<span class="count">(34)</span>
</a>
</li>

    <li class="facet" title="Rs. 5001 - Rs. 10000">
    <li class="facet" title="Rs. 10001 - Rs. 18000">
    <li class="facet" title="Rs. 18001 - Rs. 25000">
    <li class="facet" title="Rs. 25001 - Rs. 35000">
    <li class="facet" title="Rs. 35001 and Above">
    </ul>
    </div>
找不到元素:{“方法”:“css选择器”,“选择器”:“div.oneFacet bmargin5 ul”}

这可能意味着使用的选择器错误,或者找不到使用此选择器的元素。但在您的情况下,问题在于您使用的选择器

   div.oneFacet bmargin5 ul
这是指定多个类名的错误方法,请使用
句点
分隔每个类名,而不是
空格
,即

   div.oneFacet.bmargin5 ul
此外,ul有一个
id
,使用它将是一个更好的选择

   WebElement ul = driver.findElement(By.id("price_range"));


您的选择器不正确,并且您没有按照要求打印正确的数据。使用ID搜索ul,因为这是最快的方法。此外,您需要获得两个单独的文本值,为此,您必须尝试以下操作:

WebElement ul = driver.findElement(By.id("price_range"));
List<WebElement> lis = ul.findElements(By.tagName("li"));
for (WebElement li : lis) {
    System.out.println("1st part: "+li.getAttribute("title")); //To get "Rs. 2001 - Rs. 5000"
    System.out.println("2nd part: "+li.findElement(By.xpath(".//span[@class='count']")).getText()); //To get the count "(6)", if you want just "6" you can manipulate the string fetched.
}
WebElement ul=driver.findElement(By.id(“价格范围”);
列表lis=ul.findElements(按.tagName(“li”));
for(WebElement li:lis){
System.out.println(“第一部分:+li.getAttribute(“标题”);//获取“Rs.2001-Rs.5000”
System.out.println(“第二部分:+li.findElement(By.xpath(“.//span[@class='count'])));//要获取计数”(6),如果只需要“6”,可以操作获取的字符串。
}

希望对您有所帮助:)

谢谢阿米斯的解释。问题解决了Hanks Husam,对于代码,但findelement的一个问题是打印相同的计数(6),我会检查。您是说第一部分对每个li正确打印,但第二部分相同??根据您提供的html,我在xpath中找不到任何问题。Html是否准确??你能提供完整的html或URL吗?嗨,这里是链接。你可以看到下面的价格,计数在那里,找到了,第二个xpath中有一个小错误。要查看节点的子节点,我们必须从开始。/。我已经更正了上面代码中的第二个xpath。它现在应该可以正常工作了:)。此外,如果答案对你有用,你也可以接受/赞成。符号选择当前节点,若提供了任何进一步的路径(如本例中所示),它将从当前节点开始查看。另一方面,//选择文档中与所选内容匹配的节点,无论它们位于何处。因此,我们只给出//每次返回6,但当我们添加时。开始时,它每次都查看相应节点(li)的内部。
   WebElement ul = driver.findElement(By.id("price_range"));
   WebElement ul = driver.findElement(By.cssSelector("#price_range"));
WebElement ul = driver.findElement(By.id("price_range"));
List<WebElement> lis = ul.findElements(By.tagName("li"));
for (WebElement li : lis) {
    System.out.println("1st part: "+li.getAttribute("title")); //To get "Rs. 2001 - Rs. 5000"
    System.out.println("2nd part: "+li.findElement(By.xpath(".//span[@class='count']")).getText()); //To get the count "(6)", if you want just "6" you can manipulate the string fetched.
}