Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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中使用selenium提取具有多个类的列表值_Java_Selenium - Fatal编程技术网

如何在java中使用selenium提取具有多个类的列表值

如何在java中使用selenium提取具有多个类的列表值,java,selenium,Java,Selenium,HTML代码 <div id=""> <div class="widthFull floatLEFT"> <div class="class-left-first"> <div class="txt-left"> <ul class="FuelBullet bulletedArrow"> <li><a href=""></a></li&

HTML代码

<div id="">
  <div class="widthFull floatLEFT">
    <div class="class-left-first">
      <div class="txt-left">
        <ul class="FuelBullet bulletedArrow">
          <li><a href=""></a></li>
          <li><a href=""></a></li>
          <li><a href=""></a></li>
          <li><a href=""></a></li>
       </ul>
     </div>
   </div>
   <div class="class-left">
     <div class="txt-left">
       <ul class="FuelBullet bulletedArrow">
         <li><a href=""></a></li>
         <li><a href=""></a></li>
      </ul>
    </div>
  </div>
  <div class="class-left">
    <div class="txt-left">
      <ul class="FuelBullet bulletedArrow">
        <li><a href=""></a></li>
      </ul>
    </div>
  </div>
  </div>
  <div class="widthFull floatLEFT">
     ....same as above ...
  </div>
  <div class="widthFull floatLEFT">
    ....same as above ...
  </div>
</div>

……同上。。。 ……同上。。。
我的密码

WebElement ele= driver.findElement(By.id("CPH1_DisplayAllSupportedCities_PanelAllCities"));
List<WebElement> ele1=ele.findElements(By.className("widthFull floatLEFT"));

for(WebElement Element : ele1){
            List<WebElement> ele2=Element.findElements(By.className("txt-left"));
       for(WebElement txt : ele2){
                 List<WebElement> ele3=txt.findElements(By.className("FuelBullet bulletedArrow"));
           for(WebElement list : ele3){
                List<WebElement> e1=list.findElements(By.tagName("li"));
                     for(WebElement link : e1){
                     System.out.println(link.getAttribute("href"));
                     }
                 }
             }
WebElement ele=driver.findElement(By.id(“CPH1\u DisplayAllSupportedCities\u panel allcities”);
List ele1=ele.findElements(By.className(“widthFull floatLEFT”);
for(WebElement:ele1){
List ele2=Element.findElements(按.className(“txt左”);
for(WebElement txt:ele2){
List ele3=txt.findElements(按.className(“FuelBullet bulletedArrow”);
for(WebElement列表:ele3){
列表e1=列表.findElements(按.tagName(“li”));
用于(WebElement链接:e1){
System.out.println(link.getAttribute(“href”);
}
}
}
我必须从上面的示例中提取
href
值的列表。我已经尝试了上面的代码,但没有找到适合我的工作


请帮助..!!!

这里的问题是,By.className一次只接受一个类名。我们不能使用like By.className(“widthFull floatLEFT”)。 我们可以使用一个类名,比如,
By.className(“widthFull”)
或者
By.className(“floatLEFT”)

如果您想同时使用这两个类名,那么可以使用其他类型的定位器,如css选择器或xpath,如下所示

WebElement ele= driver.findElement(By.id("CPH1_DisplayAllSupportedCities_PanelAllCities"));
List<WebElement> ele1=ele.findElements(By.cssSelector(".widthFull.floatLEFT"));

for(WebElement Element : ele1){
       List<WebElement> ele2=Element.findElements(By.className("txt-left"));
       for(WebElement txt : ele2){
           List<WebElement> ele3=txt.findElements(By.cssSelector(".FuelBullet.bulletedArrow"));
           for(WebElement list : ele3){
                List<WebElement> e1=list.findElements(By.tagName("li"));
                for(WebElement link : e1){
                   System.out.println(link.getAttribute("href"));
                }
            }
       }
  }
WebElement ele=driver.findElement(By.id(“CPH1\u DisplayAllSupportedCities\u panel allcities”);
List ele1=ele.findElements(By.cssSelector(“.widthFull.floatLEFT”);
for(WebElement:ele1){
List ele2=Element.findElements(按.className(“txt左”);
for(WebElement txt:ele2){
List ele3=txt.findElements(由.cssSelector(“.FuelBullet.bulletedArrow”);
for(WebElement列表:ele3){
列表e1=列表.findElements(按.tagName(“li”));
用于(WebElement链接:e1){
System.out.println(link.getAttribute(“href”);
}
}
}
}

你能在这里分享你正在访问的元素的屏幕截图或html吗?是的,我也会在上面链接我的html内容和java代码。只需将所有值添加到
列表hrefValues
中,而不是您正在做的
System.out.println(link.getAttribute(“href”);
我必须添加但不显示(WebElement链接:e1)的输出{String str=link.getAttribute(“href”);System.out.println(str);}its使用列表显示错误