Javascript Multiple Select with Selenium
-
Hi, I am testing a webpage using selenium. I want to select all the items in a multi-select listbox. It seems selenium doesn't provide a "select all items" function even though it has an "unselect all items". I have been unable to use the add_selection function for this purpose. I write a javascript code to select all the items using eval_script(). I'm using python.
script = """
multiselect = document.getElementsByTagsName("name");
alert(multiselect);
"""The alert statement above displays that the object is an HTML Collection. Interestingly
alert(multiselect.length);
returns "0" even though I'm sure that the list is populated. This makesmultiselect.item(2)
for example to evaluate to "undefined". To select all the items I'm simply using:script = """
for(var k = 0; k < multiselect.length; k++)
{
multiselect.item(2).selected = true;
}
"""I run the script by simply:
sel.eval_script(script)
. How do I get around this? Is Javascript the best way? Thanks :)