I recently tried using the selenium RC GetAttribute method , but immediately ran into a problem. I tried to execute a very simple one selenium.GetAttribute("//a/@href"), but the code threw a SeleniumException with the message "ERROR: Could not find element attribute: // a / @ href". By substituting GetAttributeselenium.GetText("//a[@href]") for the call , I confirmed that the element is definitely present, since this operator correctly returned the link text.
Then I tried:
- pointing to another web page with a different protocol (file: /// vs http: //) is the same problem.
- using a different xpath locator pointing to a different attribute is the same problem.
- using the DOM locator
selenium.GetAttribute("document.getElementsByTagName('a')[0].getAttribute('href')")- the same problem; a little error message (and the final bracket is missing in the error message): "ERROR: Element document.getElementsByTagName ('a') [0] .getAttribute ('href' not found". Note that this exact expression works correctly in Firebug console. - using absolute instead of relative xpath addressing, c
selenium.GetText("xpath=/html/body/a[@href]")to confirm the existence, and then selenium.GetAttribute("xpath=/html/body/a/@href")to get the attribute - and it will work!
While the manual clearly states that relative xpath locators do not need an explicit locator type (that is, the prefix "xpath ="), it is silent about absolute xpath locators ; I interpret from this that a prefix is required. But out of curiosity, I returned to my relative expression and added an explicit prefix - changed selenium.GetAttribute("//a/@href")to selenium.GetAttribute("xpath=//a/@href")- and it worked too!
Finally, my experiments with the very convenient Find button in the Selenium IDE show that it works great with elements, but not with attributes. I can understand that it makes no sense to highlight an attribute, since attributes are not visible elements of the page, but why not select the element containing this attribute and make it a different color? Perhaps not a trivial task ...
:
; ! , , , :
- GetAttribute XPath , (, GetText)?
- DOM " "? ( , DOM , "dom =" DOM, .)
- Selenium IDE , () ?
"xpath=//a/@href" "" : " [error] : xpath =//a/@href, error = [Exception..." JavaScript arg 0 [ inIFlasher.scrollElementIntoView] "nsresult:" 0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS) ":" JS frame:: chrome://selenium-ide/content/selenium-runner.js:: showElement:: 386 "data: no]"
: (A) GetText (locator-for-element-with-attribute), (B) GetAttribute ( ). 6 3 , , , , . ?
Type GetText GetAttribute
XPath //a[@href] xpath=//a/@href
CSS css=a[href] ??
DOM ?? document.getElementsByTagName('a')[0].getAttribute('href')
(: Selenium RC 1.0.3, : Firefox 3.6.13, #)