gpt4 book ai didi

java - 如何从标签中获取所有元素

转载 作者:行者123 更新时间:2023-12-01 13:31:12 24 4
gpt4 key购买 nike

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

我需要获取值

卢比。 2000 - 卢比。下面
卢比。 2001 - 卢比。 5000
卢比。 5001-卢比。 10000
卢比。 10001 - 卢比。 18000卢比。 18001 - 卢比。 25000卢比。 25001 - 卢比。 35000卢比。 35001及以上

和值 6 34 41 12 15 9 4

下面是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>

iam 使用以下代码

WebElement ul = driver.findElement(By.cssSelector("div.oneFacet bmargin5 ul"));
List<WebElement> lis = ul.findElements(By.tagName("li"));
for (WebElement li : lis) {
System.out.println("names "+li.getText());
}

但出现错误。

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"div.oneFacet bmargin5 ul"}
Command duration or timeout: 30.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'VALUED-8JGOG5PH', ip: '192.168.1.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'
Session ID: 98a38901-4a9b-4aef-8823-b96f9043f5ad
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=26.0}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
etc....

最佳答案

您的选择器不正确,而且您没有按照您的要求打印正确的数据。使用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.
}

希望对你有帮助:)

关于java - 如何从标签中获取所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21558254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com