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("Add Labor Item", '/quote_items/new?item_type=labor&product_category_id=1912275102536303582&quote_id=1949426380886245765')" href="javascript:void(0)">Labor</a>
</li>
<li>
<a class="pull-left" onclick="showPopup("Add Parts Item", '/quote_items/new?item_type=part&product_category_id=1912275102796350432&quote_id=1949426380886245765')" href="javascript:void(0)">Parts</a>
</li>
<li>
<a class="pull-left" onclick="showPopup("Add Warranties Item", '/quote_items/new?item_type=warranty&product_category_id=1912275103022842850&quote_id=1949426380886245765')" href="javascript:void(0)">Warranties</a>
</li>
</ul>
</div>
-
1What happens when you change driver.findElements(By.className("dropdown-menu")); to use the "pull-left" class?Chris Kenst– Chris Kenst2019年01月04日 18:32:30 +00:00Commented Jan 4, 2019 at 18:32
1 Answer 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.
Explore related questions
See similar questions with these tags.