Reputation: 13
Below is my code. I want to click the anchor tag <a>
, which is inside the <li>
. I have tried to get the value using By.tagName("//li/a")
. I didn't get any data.
There are many lists with class="dropdown-menu"
in my HTML. I should be able to select a single list from which to click the drop down values.
<ul class="dropdown-menu" style="position: static; margin-bottom: 5px; *width: 50px;border-style: none;min-width: 45px;box-shadow: 0 0px;text-align: center;" aria-labelledby="dropdownMenu" role="menu">
<li><a class="dropdown-add" style="cursor: pointer;">1</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">2</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">3</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">4</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">5</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">6</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">7</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">8</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">9</a></li>
<li><a class="dropdown-add" style="cursor: pointer;">10</a></li>
</ul>
Upvotes: 1
Views: 5719
Reputation: 27496
It's easy to get list of all anchors, just use CSS Selectors
List<WebElement> anchors = webDriver.findElement(By.cssSelector("ul li a"));
Then choose whatever anchor you want to click at, let's say
anchors.findElement(By.linkText("10")).click();
Upvotes: 5
Reputation: 7743
First get the NodeList of the anchors:
var res = document.querySelectorAll('ul.dropdown-menu li a.dropdown-add');
Then force a click
on the 3rd, if it's not empty:
if(res[2].childNodes[0].nodeValue.length) { res[2].click() };
Upvotes: -1