0

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

1
  • Note: Code has been edited from original question; the exception has also changed: WebDriverException: Cannot click on option element. The new lines added to original question posted are: WebElement listbox_element; listbox_element = driver.findElement(By.cssSelector("span[aria-owns='ddlRegion_listbox']")); listbox_element.click(); Commented Aug 12, 2016 at 18:26

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Grasshopper: I'm new to Selenium, so I got some of that. Here is the code that I added before the code I shown before to achieve the click and make stuff visible before trying to select an option, but now I get a different exception: Cannot click an option element. Here is the code I added before the code shown: WebElement listbox_element; listbox_element = driver.findElement(By.cssSelector("span[aria-owns='ddlRegion_listbox']")); listbox_element.click(); <Followed by the code I shared before>
your code works :) how do you figure out what goes inside the xpath? looks Chinese. is there some instruction manual on how to figure what I need to do there, or maybe I need to go learn more html or something? thanks a lot! I will accept the answer later. I want to keep the question open for any comments that anybody might want to share, then later i'll accept the answer if nobody has anything else to say.
Selenium is all about locating the HTML element you want to play with. Tht can be done with the id, class etc. Gets crappy if you dont have those, then you have to use CSS or xpath. If you have done any webpage work CSS should be familiar. Else you got to pick it up. Most of the CSS and xpath can be got from browser and tools like firebug in firefox. But you go to fine tune them. Happy learning.
Grasshopper, I got a totally different Xpath from Firefox for "All" checkbox, using techniques I found in video. I got xpath: /html/body/div[1]/div[3]/div/div/div[19]/div[1]/ul/li[1]/input FOR: <input type="checkbox" value="0" id="chk_region_0"> USING Firebug (looks REALY nice and easy) and techniques I found in youtube.com/watch?v=ohY815wUz9o Any comments on this? I don't know if this one works or not yet, but yours works. Trying to fit this puzzle together. It would be nice to have a good way of finding xpath all the time, like mentioned in vdo.
What firebug gives is an absolute path (starts with /) which traces the whole structure of the page starting from html tag. Use this as a very basic way of getting xpaths. Imagine the developer of the page modifies the page and inserts a new element in the path you got from firebug.Your test will be shot and you will need to repeat.Best is to use a relative path (starts with //) by identifying the element directly using an id (most preferred) or any unique attribute, if not going one level up and repeating the identifying part.Try to keep the path as short so change does not mess it up easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.