This is primarily for Selenium RC, but should be adaptable for Selenium 2 / Web Driver.
I recently stumbled across this. If ever one has issues validating element state such as if element is checked or selected, or what item in menu is the selected item. There are some alternate ways to validate the element state other than the given default Selenium API methods.
For example using CSS selectors, particularly the Sizzle/JQuery engine:
result = selenium.isElementPresent(“css=input[id*=’chkbxId’]:checked”);
Will either return true if the element/checkbox is checked or false if not. If using other Selenium methods against this selector, it will either work if element is checked or fail if not checked because element is not found because per the selector rules, it is not checked.
And for getting the selected text/option in a select list element, the following may work:
result = selenium.getText(“css=select[name=’selectElementName’] > option:selected”);
Which will return text of the option element that is selected and is also a child of the specified select element.
result = selenium.getEval(“this.browserbot.findElement(\”id=chkboxId\”).checked”);
And for getting the selected text/option in a select list element, the following might work:
index = selenium.getEval(“this.browserbot.findElement(\”id=select Element Id\”).selectedIndex”);
result = selenium.getEval(“this.browserbot.findElement(\”id=selectElementId\”)[” + index + “].value”);
I didn’t fully test out the select list element methods above, but I did test out the checked elements for checkbox and it does work.
These ideas were adapted from web development concepts using the DOM and CSS selectors, for cases where checking element state is problematic in Selenium for whatever reason. References I used for the adaptation:
Thought I’d provide some context examples for WebDriver, for the DOM examples above, you can adapt to WebDriver as follows
where element is a select list element and we want the selected option index