Selenium的GetAttribute方法的并发症

我最近尝试使用selenium RC的GetAttribute方法,但立即遇到了挑战。 我试图执行一个非常简单的selenium.GetAttribute("//a/@href")但代码抛出了一个SeleniumException,消息为“ERROR:找不到元素属性:// a / @ href” 。 通过用selenium.GetText("//a[@href]")代替GetAttribute调用,我确认一个元素肯定存在,因为这个语句正确地返回了链接的文本。

然后我尝试了:

  • 指向具有不同协议的不同网页(文件:/// vs http://) – 同样的问题。
  • 使用指向不同属性的不同xpath定位器 – 同样的问题。
  • 使用DOM定位器selenium.GetAttribute("document.getElementsByTagName('a')[0].getAttribute('href')") – 同样的问题; 稍有不同的错误消息(并且错误消息缺少最后的括号): “错误:元素document.getElementsByTagName(’a’)[0] .getAttribute(’href’not found” 。请注意,此精确表达式在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中使用非常方便的“ 查找”按钮进行的​​实验表明,它可以很好地处理元素,但是会失败并带有属性。 我可以理解,突出属性没有意义,因为属性不是可见的页面元素,但为什么不突出显示包含属性的元素,并使其以不同的颜色? 也许不是一项微不足道的任务……

我的问题:

我将上述实验的结果归结为这些问题; 这是我在这里发布的全部目的! 这些似乎对我来说都是一个错误,但如果您认为我的使用不正确或有解决方法,请告诉我:

  1. 为什么具有XPath定位器类型的GetAttribute在其他方法(例如GetText)不需要时才需要显式定位器类型?
  2. 为什么DOM定位器因“未找到”错误而失败? (该手册还明确指出DOM定位器不需要显式的定位器类型前缀,但我仍然尝试在DOM测试中添加“dom =”作为前缀;它仍然失败。)
  3. 为什么Selenium IDE在尝试突出显示(查找)属性时不会更优雅地失败? 使用相同的"xpath=//a/@href"定位器,按下查找按钮会产生这个丑陋的消息:“找不到[错误]定位器:xpath = // a / @ href,error = [Exception …”可能不转换JavaScript参数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“数据:没有]“

您还需要输入以下内容:此处我想要的每个测试模式是(A) GetTextlocator-for-element-with-attribute )确认元素的存在然后(B) GetAttributelocator-for-)属性 – 本身 )。 在下表中的6个插槽中,我成功地解决了其中3个插槽,并且第4个似乎是一个错误。 剩下的两个插槽有解决方案吗?

键入GetText GetAttribute
 XPath //一个[@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#中的目标代码)

Selenium RC的GetAttribute方法返回element \ attribute定位符的 。 这些定位器的一般forms是

 "[locator (id, xpath, css, etc)]@[attribute name]" 

例如

 "SaveButton@href" 

返回具有id SaveButton的元素上的href属性的值。 也可以使用Xpath定位器:

 "xpath=//a[contains(@id, 'SaveButton')]@href" 

返回其id包含文本SaveButton的元素的href属性的值。

要回答你的问题,

  • 1:我真的不知道,这是Selenium设计师的问题。

  • 2:Selenium命令执行几个不同的“上下文”。 在一些命令中, document是指被测试的网页,在其他命令中, document是指包含Selenium框架的页面(我相信testRunner.html)。

  • 3:错误消息表明它找不到您请求的元素。 如果实际存在错误,那么之后的信息可能对Selenium团队有用,但并不会对您产生影响。 信息越多越好,对吧?

http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html#getAttribute%28java.lang.String%29

 getAttribute java.lang.String getAttribute(java.lang.String attributeLocator) Gets the value of an element attribute. Parameters: attributeLocator - an element locator followed by an @ sign and then the name of the attribute, eg "foo@bar" Returns: the value of the specified attribute 

所以你应该说selenium.GetAttribute("locator@href")定位器是id或name。 如果你的元素没有id或name,你应该使用xpath, selenium.GetAttribute("xpath=/html/body/a/@href")就像你已经成功尝试过一样。