Selenium 的 GetAttribute 方法的并发症
我最近尝试使用 selenium RC 的 GetAttribute 方法,但立即遇到了挑战。我尝试执行一个非常简单的 selenium.GetAttribute("//a/@href")
但代码抛出了 SeleniumException 并显示消息 "ERROR: Could not find element attribute: // a/@href”。 通过用 selenium.GetText("//a[@href]")
代替 GetAttribute 调用,我确认了一个元素确实存在,因为此语句正确返回链接的文本。
然后我尝试:
- 指向具有不同协议的不同网页(file:/// vs http://)——同样的问题。
- 使用指向不同属性的不同 xpath 定位器——同样的问题。
- 使用 DOM 定位器
selenium.GetAttribute("document.getElementsByTagName('a')[0].getAttribute('href')")
-- 同样的问题;错误消息略有不同(并且错误消息缺少最后的括号):“错误:Element document.getElementsByTagName('a')[0].getAttribute('href' not find”。请注意这个确切的表达式在 Firebug 的控制台中 - 使用绝对而不是相对 xpath 寻址正确工作,使用 selenium.GetText("xpath=/html/body/a[@href]") 来确认存在,然后
selenium.GetAttribute("xpath=/html/body/a/@href")
获取属性 - 并且它有效,
尽管手册明确指出相对 xpath定位器不需要显式定位器类型(即“xpath=”前缀),它对 绝对 xpath 定位器保持沉默;我由此解释为需要前缀,但出于好奇,我回去了。到我的相对表达式并添加显式前缀 - 将 selenium.GetAttribute("//a/@href")
更改为 selenium.GetAttribute("xpath=//a/@href" )
——这也有效!
最后,我对 Selenium IDE 中非常方便的 Find 按钮的实验表明,它对元素处理得很好,但对属性却失败了。我可以理解,突出显示属性没有意义,因为属性不是可见的页面元素,但为什么不突出显示包含该属性的元素,并使其采用不同的颜色呢?也许不是一个微不足道的任务......
我的问题:
我将上述实验的结果提炼为这些问题;这就是我在这里发帖的全部目的!这些对我来说似乎都是一个错误,但如果您认为我的用法不正确或有解决方法,请告诉我:
- Why does GetAttribute with an XPath locator type uniquely require an explicit locator type while othermethods (例如GetText)不可以吗?
- 为什么 DOM 定位器会失败并出现“未找到”错误? (手册还明确指出 DOM 定位器不需要显式定位器类型前缀,但我仍然尝试在 DOM 测试中添加“dom=”作为前缀;它仍然失败。)
- 为什么 Selenium当尝试突出显示(查找)属性时,IDE 不会更优雅地失败吗?使用相同的“xpath=//a/@href”定位器,按下“查找”按钮会产生以下丑陋的消息:“[错误]定位器未找到:xpath=//a/@href,错误= [异常...“无法转换 JavaScript 参数 arg 0 [inIFlasher.scrollElementIntoView]” nsresult:“0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)”位置:“JS 框架 :: chrome://selenium-ide/content/selenium-runner.js : : showElement :: line 386" data: no]"
您还需要输入以下内容:我想要的每个测试的模式是 (A) GetText(locator-for-element -with-attribute)来确认元素是否存在,然后 (B) GetAttribute(locator-for-attribute-itself)。在下表中的 6 个插槽中,我成功解决了其中的 3 个,正如详细说明的那样,第 4 个似乎是一个错误。 剩下的两个槽有解决方案吗?
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,我的C#目标代码)
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...
My questions:
I distilled the results of the above experiments down to these questions; this is the whole purpose of my posting here! Each of these seems like a bug to me but let me know if you think my usage is incorrect or there is a workaround:
- Why does GetAttribute with an XPath locator type uniquely require an explicit locator type when other methods (e.g. GetText) do not?
- Why did the DOM locator fail with a 'not found' error? (The manual also clearly states that DOM locators do not require an explicit locator type prefix, but I nevertheless tried adding "dom=" as a prefix on the DOM test; it still failed.)
- Why does Selenium IDE not fail more gracefully when attempting to highlight (Find) an attribute? With the same
"xpath=//a/@href"
locator, pressing the Find button yields this ugly message: "[error] locator not found: xpath=//a/@href, error = [Exception... "Could not convert JavaScript argument arg 0 [inIFlasher.scrollElementIntoView]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: chrome://selenium-ide/content/selenium-runner.js :: showElement :: line 386" data: no]"
Your input is also requested for the following: The pattern I wanted for each test here was (A) GetText(locator-for-element-with-attribute) to confirm the presence of the element then (B) GetAttribute(locator-for-attribute-itself). Of the 6 slots in the table below I solved 3 of them successfully as just detailed, and a 4th seems to be a bug. Are there solutions for the remaining two slots?
Type GetText GetAttribute XPath //a[@href] xpath=//a/@href CSS css=a[href] ?? DOM ?? document.getElementsByTagName('a')[0].getAttribute('href')
(Details: Selenium RC version 1.0.3, Browser: Firefox 3.6.13, My target code in C#)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Selenium RC 的
GetAttribute
方法返回元素\属性定位器的值。 这些定位器的一般形式是例如,
返回 id 为
SaveButton
的元素上的href
属性的值。还可以使用 Xpath 定位器:返回 id 包含文本
SaveButton
的元素的href
属性值。回答你的问题,
1:我真的不知道,这是 Selenium 设计者的问题。
2:Selenium 命令执行几个不同的“上下文”。在某些命令中,
document
指的是被测试的网页,在其他命令中,document
指的是包含 Selenium 框架的页面(我相信是 testRunner.html)。3:错误消息表明找不到您请求的元素。如果确实存在错误,那么之后的信息可能对 Selenium 团队有用,但不会真正影响您。信息越多越好,对吗?
Selenium RC's
GetAttribute
method returns the value of the element\attribute locator. The general form for these locators isfor example
returns the value of the
href
attribute on the element with the idSaveButton
. Xpath locators can also be used:returns the value of the
href
attribute on the element whose id contains the textSaveButton
.To answer your questions,
1: I don't really know, this is a question for the designers of Selenium.
2: The Selenium commands execute several different "contexts". In some of the commands,
document
refers to the web page under test, in other commands,document
refers to the page containing the Selenium frame (testRunner.html I believe).3: The error message says that it cannot find the element you requested. The information after that could be useful to the Selenium team if there is actually a bug present, but doesn't really affect you. The more information the better, right?
http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html#getAttribute%28java.lang.String%29
所以你应该说
selenium.GetAttribute("locator@href")
Locator 是 id 或 name。如果您的 a 元素没有 id 或名称,您应该使用 xpath,selenium.GetAttribute("xpath=/html/body/a/@href")
就像您已经成功尝试过的那样。http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html#getAttribute%28java.lang.String%29
So you should say
selenium.GetAttribute("locator@href")
Locator being id or name. If your a element does not have id or name, you should use xpath,selenium.GetAttribute("xpath=/html/body/a/@href")
like you already successfully tried.