从“行项目”列表中提取值

时间:2018-03-08 12:21:09

标签: java selenium selenium-webdriver selenium-chromedriver

我有一个'LI'(specificationItem)项列表,我需要提取它的值。我需要的值在

如何返回所有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>

2 个答案:

答案 0 :(得分:1)

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:background="#ccc"
      />
  <VideoView
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      />
</LinearLayout>

答案 1 :(得分:0)

当您使用基于类名的css选择器并且有多个元素时,将返回元素列表。获得此列表后,您可以迭代它并检索值。像这样:

List<WebElement> elements = driver.findElements(By.cssSelector(".specificationItem"));
for(WebElement element : elements) {
    WebElement value = element.findElements(By.cssSelector(".valueContent"));
    System.out.println(value.getText());
}

上面的代码将使用classname specificationItem 遍历所有webelements,并使用classname valueContent 提取每个webelement。然后它将打印项目的内部文本。

如果您不关心li元素,则不必迭代li元素,您可以执行以下操作:

List<WebElement> elements = driver.findElements(By.cssSelector(".valueContent"));
elements.forEach(System.out::println);