How to Extract the list value having the multiple classes using selenium in java

–2 votes
asked Sep 13, 2017 by elakiya

HTML Code

<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>

My tried Code.

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"));
                     }
                 }
             }

I have to extract the list of href value from above sample. I have tried above code, but it didn't get work for me.

Please Help..!!!

1 Answer

–1 vote
answered Sep 13, 2017 by murthi

The problem here is that, the By.className accepts only one class name at a time. We can not use like By.className("widthFull floatLEFT"). We can use either one class name like, By.className("widthFull") or By.className("floatLEFT").

if you want to use both class names then, you can go with other type of locator like css selector or xpath as given below.

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"));
                }
            }
       }
  }
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...