3

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>
c32hedge
2,70920 silver badges39 bronze badges
asked Jan 4, 2019 at 13:54
1
  • 1
    What happens when you change driver.findElements(By.className("dropdown-menu")); to use the "pull-left" class? Commented Jan 4, 2019 at 18:32

1 Answer 1

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.

answered Jan 4, 2019 at 19:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.