mansi Agrawal
mansi Agrawal

Reputation: 43

How to select an element using selenium which does not contain a particular class

I have the following HTML code.

<ul class="options">
    <li class="first popover-options ">data</li>
    <li class="first popover-options disabled">data2</li>
    <li class="first popover-options ">data3</li>
</ul>

I need to select the element with class="first popover-options", that is it should not contain disabled. How to do this using selenium in java?

Upvotes: 2

Views: 1979

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193088

As per your question to identify any of the element with class="first popover-options" which should not contain disabled as per the HTML you have shared you can create a List of the elements matching the criteria using either of the following solutions:

  • Using cssSelector:

    for (WebElement element:driver.findElements(By.cssSelector("ul.options>li.first.popover-options:not(.disabled)"))) 
    {
        //perform any action with the elements from the list
    }
    
  • Using xpath:

    for (WebElement element:driver.findElements(By.xpath("//ul[@class='options']//li[@class='first popover-options']"))) 
    {
        //perform any action with the elements from the list
    }
    

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

You can try this code :

List<WebElement> ListEle=  driver.findElements(By.cssSelector("li.first popover-options"));
for (WebElement tempEle : ListEle) {
    if(tempEle.getText().contains("Data"))
     {
       tempEle.click();
     }    
   }

Note that it will select Data from the drop down , if you want to select any other item from drop down , you can have it by replacing the code at this level : tempEle.getText().contains("Data")

Upvotes: 0

iamsankalp89
iamsankalp89

Reputation: 4739

You can select multiple element using LIST and then choose operation on your selected items.

Sample code is like this:

List<WebElement> ListEle=  driver.findElements(By.xpath("//ul[@class='options']//li[@class='first popover-options']"));
for (WebElement tempEle : ListEle) {
    if(conditions)
     {
       //statemnets
       Break;
     }    
   }

Upvotes: 0

Related Questions