gpt4 book ai didi

java - 从行项目列表中提取值

转载 作者:行者123 更新时间:2023-11-30 06:16:05 25 4
gpt4 key购买 nike

我有一个“LI”(规范项)项目列表,我需要提取其值。我需要的值在

如何返回所有 LI 值并捕获这些值?

                <div class="vehicle-features">
<h2 class="vehicle-features_header">Specification</h2>

<div class="specificationList">
<ul class="inner">

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_0" class="odometer">Odometer</span>
</dt>
<dd class="value">
<span class="valueContent">11,984 miles</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_1" class="colourGroup">Colour Group</span>
</dt>
<dd class="value">
<span class="valueContent">Blue</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_2" class="fuelType">Fuel Type</span>
</dt>
<dd class="value">
<span class="valueContent">Diesel</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_3" class="transmission">Transmission</span>
</dt>
<dd class="value">
<span class="valueContent">Automatic</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_4" class="bodyStyle">Body Style</span>
</dt>
<dd class="value">
<span class="valueContent">Hatchback</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_5" class="co2">CO2</span>
</dt>
<dd class="value">
<span class="valueContent">114 g/km</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_6" class="milesPerGallon">Miles Per Gallon</span>
</dt>
<dd class="value">
<span class="valueContent">64.2 mpg</span>
</dd>
</dl>
</li>

<li class="specificationItem">
<dl>
<dt class="icon">
<span id="Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_8" class="trimColour">Trim Colour</span>
</dt>
<dd class="value">
<span class="valueContent">black cloth</span>
</dd>
</dl>
</li>
</ul>
</div>
</div>
</div>
</div>

最佳答案

要检索所有specationItem和相应的,您可以使用以下代码块:

List<String> specificationItems = new ArrayList<String>();
List<String> specificationValues = new ArrayList<String>();
List<WebElement> all_specificationItems = driver.findElements(By.xpath("//div[@class='specificationList']/ul[@class='inner']//li[@class='specificationItem']/dl/dt[@class='icon']/span[starts-with(@id,'Body_ContentPlaceHolderContent_ContentPlaceHolderContent_VehicleFeatures_FeatureList_FeatureText_')]"));
List<WebElement> all_specificationValues = driver.findElements(By.xpath("//div[@class='specificationList']/ul[@class='inner']//li[@class='specificationItem']/dl//span[@class='valueContent']"));
int size = all_specificationItems.size();
for(WebElement ele:all_specificationItems)
specificationItems.add(ele.getAttribute("innerHTML"));
for(WebElement elem:all_specificationValues)
specificationValues.add(elem.getAttribute("innerHTML"));
for(int i=0;i<size;i++)
System.out.println(specificationItems.get(i) + " has a value of " + specificationValues.get(i));

关于java - 从行项目列表中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49172983/

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