Reputation: 377
I wanted to select and click one of the option which becomes visible through drop down list when the mouse moves to the text or visible text on a web page .
So basically I move the mouse to text "Browse" in the below html page and then I get a different options like "Pages", "Blog".
So in order to select or click "Browse" I use (in Selenium 2.21.0) :
driver.findElement(By.xpath("//a[@id='browse-menu-link']/span")).click()
.
Is there a way that I can select the rest of the options without knowing their ids (as their ids can change or name can change).
Can I move and select different options by index?
I.e my requirement is there can be 4 options or 5 option in the drop down.I don't care about their name as long as they are under "Browse" Text.
In the html page, these hidden options which become visible when mouse moves or is clicked at "Browse" have class name as:
ajs-drop-down hidden
There are also other hidden options under some other drop down having same class name so selecting elements by class name gives error.
Here is the HTML code/page:
<head>
<body id="com-test" class="" onload="placeFocus()">
<div id="full-height-container">
<fieldset class="hidden parameters">
<fieldset class="hidden parameters">
<div id="header" class="">
<form id="quick-search" class="quick-search" action="/dosearchsite.action" method="get">
<fieldset>
<legend>Quick Search</legend>
<input id="quick-search-query" class="quick-search-query placeholded" type="text" size="25" name="queryString" autocomplete="off" accesskey="q">
<input id="quick-search-submit" class="quick-search-submit" type="submit" value="Search">
<div class="aui-dd-parent quick-nav-drop-down"></div>
</fieldset>
<fieldset class="hidden parameters">
<input id="quickNavEnabled" type="hidden" value="true">
</fieldset>
</form >
<ul id="header-menu-bar" class="ajs-menu-bar">
<li class="normal ajs-menu-item">
<a id="browse-menu-link" class="browse trigger ajs-menu-title" href="#">
<span>
**<span>Browse</span>**
</span>
</a>
<div class="ajs-drop-down hidden" hidden="" style="">
<ul id="browse-menu-link-leading" class="section-leading first">
<li class="">
<a id="space-pages-link" class="" title="Attachments: 5" href="/pages/listpages.action? key=UNI">
**<span>Pages</span>**
</a>
</li>
<li class="">
<a id="space-blogposts-link" class="" title="Attachments: 5" href="/pages/viewrecentblogposts.action?key=UNI">
**<span>Blog</span>**
</a>
Upvotes: 0
Views: 2566
Reputation: 4099
Let me just make sure: after hovering mouse on "Browse" link the items under < div class="ajs-drop-down hidden" hidden="" style=""> become visible and you want to get to them using indexes instead of any names?
In such case I would do the following
List<WebElemet> menuItems = driver.findElement(By.xpath("//*[@class='ajs-drop-down hidden']")).findElements(By.tagName("a"));
WebElement exampleItem = menuItems.get(1); //getting 2nd menu item
You can also play with some ugly xpath
WebElement exampleItem = driver.findElement(By.xpath(//div[@className='ajs-drop-down hidden'/ul/li[1]/a]));
I recommend the first method however.
Now, you mentioned something about other options under other dropdowns. So i assume that locating appropriate div like in example above is not so straightforward. If that's the case we need to first locate the div using link's (a element) id.
List<WebElemet> menuItems = driver.findElement(By.xpath("//a[@id='browse-menu-link']/../div")).findElements(By.tagName("a"));
That should yield the same results as the first example.
EDIT
If you want to find an element using just a part of class name, the xpath is your friend:
List<WebElemet> menuItems = driver.findElement(By.xpath("//*[contains(@class, 'hidden')]")).findElements(By.tagName("a"));
driver.findElement will return the first element that meets specified criteria. If you want to get 2nd or 3rd just use findElement*s* and get the element from the list (like in my first example above)
Upvotes: 1