2

Here is code that I am using firstly I am clicking on the button then the list of items is shown and I try to select from the list but it said that Index=0, Size=0 when I use Xpath. When I use the class_name it said "Element not Visible". Here is code for this:

driver.findElement(By.id("add_new_item_btn")).click();
Thread.sleep(6000);
java.util.List<WebElement> listItems1 = driver.findElements(By.className("dropdown-menu"));
listItems1.get(0).click();

Here is the code in the the inspect element:

<div class="dropdown open">
    <button class="btn-blue-simple btn dropdown-toggle" type="button" id="add_new_item_btn" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
      Add
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">

        <li>
          <a class="pull-left" onclick="showPopup(&quot;Add Labor Item&quot;, '/quote_items/new?item_type=labor&amp;product_category_id=1912275102536303582&amp;quote_id=1949426380886245765')" href="javascript:void(0)">Labor</a>
        </li>
        <li>
          <a class="pull-left" onclick="showPopup(&quot;Add Parts Item&quot;, '/quote_items/new?item_type=part&amp;product_category_id=1912275102796350432&amp;quote_id=1949426380886245765')" href="javascript:void(0)">Parts</a>
        </li>
        <li>
          <a class="pull-left" onclick="showPopup(&quot;Add Warranties Item&quot;, '/quote_items/new?item_type=warranty&amp;product_category_id=1912275103022842850&amp;quote_id=1949426380886245765')" href="javascript:void(0)">Warranties</a>
        </li>
    </ul>
</div>
  • 1
    What happens when you change driver.findElements(By.className("dropdown-menu")); to use the "pull-left" class? – Chris Kenst Jan 4 at 18:32
1

It seems you're looking at the upper hierarchy in the DOM. Try the below code; the intent is to click on the first index.

driver.findElement(By.id("add_new_item_btn")).click();
// some explicit wait goes here
List<WebElement> dropdown_items = new ArrayList<>();
dropdown_items = driver.findElements(By.xpath("//div[@class='dropdown open']//li//a"));
dropdown_items.get(0).click()

After this proof works, you might need to refactor this to a method to imitate the behavior of an actual Select class over your dropdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.