I recently attempted to use selenium RC's GetAttribute method but immediately ran into a challenge. I tried to execute a very simple
selenium.GetAttribute("//a/@href") but the code threw a SeleniumException with the message "ERROR: Could not find element attribute: //a/@href". By substituting selenium.GetText("//a[@href]") in place of the GetAttribute call, I confirmed that an element was definitely present, as this statement properly returned the text of the link.
I then tried:
- pointing to a different web page with a different protocol (file:/// vs http://) -- same problem.
- using a different xpath locator pointing to a different attribute -- same problem.
- using a DOM locator
selenium.GetAttribute("document.getElementsByTagName('a')[0].getAttribute('href')")-- same problem; slightly different error message (and the error message is missing the final parenthesis): "ERROR: Element document.getElementsByTagName('a')[0].getAttribute('href' not found". Note that this exact expression works correctly in Firebug's console. - using absolute instead of relative xpath addressing, with
selenium.GetText("xpath=/html/body/a[@href]")to confirm the existence and thenselenium.GetAttribute("xpath=/html/body/a/@href")to get the attribute -- and it worked!
While the manual clearly states that relative xpath locators do not need an explicit locator type (i.e. the "xpath=" prefix) it is silent about absolute xpath locators; I interpret from this that the prefix is required. But out of curiousity, I went back to my relative expression and added the explicit prefix--changing
selenium.GetAttribute("//a/@href") to selenium.GetAttribute("xpath=//a/@href") -- and this also worked!
Finally, my experiments with the very handy Find button in Selenium IDE show that it does fine with elements but fails with attributes. I can understand that it is not meaningful to highlight an attribute since attributes are not visible page elements, but why not highlight the element containing the attribute, and make it in a different color? Perhaps not a trivial task...
No comments:
Post a Comment