Reputation: 125
Trying to select from multi-select dropdown box and found what seemed to be a good resource.
I am working on http://preview.harriscountyfws.org/ I am trying to Select multiple items on the "Select by Agency" mutli-select dropdown.
The logic I show you here is pretty straight up and follows the article I found (shown below), but I get WebDriverException: Cannot click on option element.
Any suggestions to get a logic that works?
Here is the logic:
WebElement we;
String searchText;
WebDriver driver;
Select select;
WebElement listbox_element;
listbox_element = driver.findElement(By.cssSelector("span[aria-owns='ddlRegion_listbox']"));
listbox_element.click();
driver = new FirefoxDriver();
driver.get("http://preview.harriscountyfws.org/");
searchText="ALL";
we = driver.findElement(By.id("ddlRegion"));
select = new Select(we);
select.selectByVisibleText(searchText);
REF: http://www.techbeamers.com/dropdown-and-multiple-select-in-webdriver
Upvotes: 0
Views: 1453
Reputation: 9058
I dont think you are finding the right select element. One you are trying is not in visible state but hidden (visible: none).
You need to locate the arrow for the Search By Agency dropdown and find the element and click on it to make the dropdown visible. I am not sure of including the aria-owns attribute in xpath but that is the easy way out. You can have a look at it.
"//div[@id='searchDiv']//span[@aria-owns='ddlRegion_listbox']//span[@class='k-select']"
Then you need to wait for the div[@id='regionSelectPopup'] to be visible. Stick it in a webdriverwait with Expectedcondition of visibility.
Then you can choose the option you want in the div. I have done for ALL. You will need to parameterize it. Click on it.
"//div[@id='regionSelectPopup']//label[.='ALL']/preceding-sibling::input[@type='checkbox']"
It may work with the label too and not finding the checkbox.
Hope this works.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://preview.harriscountyfws.org/");
WebElement agencySearchSelect = driver.findElement(
By.xpath("//div[@id='searchDiv']//span[@aria-owns='ddlRegion_listbox']//span[@class='k-select']"));
agencySearchSelect.click();
new WebDriverWait(driver, 3, 100).until(ExpectedConditions.visibilityOfElementLocated(
By.id("regionSelectPopup")));
WebElement agencyOption = driver.findElement(
By.xpath("//div[@id='regionSelectPopup']//label[.='ALL']/preceding-sibling::input[@type='checkbox']"));
agencyOption.click();
Upvotes: 1