Reputation: 697
I have a web form having JQuery dropdowns. The particular field holds date of birth. The source for the field is:
<div class="tooltipGroup" style="z-index:19;">
<div class="day">
<div class="jqTransformSelectWrapper" style="z-index: 19;">
<div>
<ul style="width: 100%; display: block; visibility: visible;">
<li class="optHeading">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<a index="6" href="#">6</a>
</li>
<li class="undefined">
<a index="31" href="#">31</a>
</li>
That's the code trying to get all of the elements and put them in a HashMap:
public void selectDob(int dob) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
dropdownValues.get(dob).click();
}
The code works just fine with one exception: it can't get the values of all fields, just the first visible when the dropdown is being opened.
1 2 3 4 5
The question is how to get the values of the other fields?
Upvotes: 2
Views: 2565
Reputation: 697
I would like to thank you guys for your help (especially HemChe).
It turned out that this is a bug in the latest FirefoxDriver version 2.32. The same code worked just fine with ChromeDriver and I've got all of the drropdown values. Downgrading the selenium version to 2.31 solved that problem and the code works with both drivers!
I'll register a bug on the Selenium bug tracker!
That's the final version of my code:
public void selectFromDropdown(String option) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText().toString());
}
dropdownValues.get(option).click();
}
Cheers!
Upvotes: 3
Reputation: 2337
Try the below code and check if it is working.
WebElement w = driver.findElement(By.id("aWrapper_dob_day"));
w.click();
WebElement dobFieldDropdown = driver.findElements(By.className("undefined"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
Upvotes: 2
Reputation: 27516
You can't locate invisible elements with Web Driver, you need to use JavaScript in order to obtain them. So try something like
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("get them here by class name");
Upvotes: 1