硒的getAttribute方法并发症 [英] Complications with Selenium's GetAttribute method

查看:209
本文介绍了硒的getAttribute方法并发症的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试使用硒RC的的getAttribute 的方法,但很快就遇到了一个挑战。我试图执行一个非常简单的 selenium.GetAttribute(//一/ @ HREF)但是代码扔SeleniumException与消息的错误:无法发现元素属性:// A / @ HREF的。
通过在地方的的getAttribute 电话,我证实,代 selenium.GetText(//一个[@href]) 。一个元素肯定存在,因为这种说法正确的返回链接的文本

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.

然后我尝试:


  • 指向一个不同的网页,用不同的协议(文件:/// VS HTTP://) - 同样的问题

  • 使用不同的XPath定位器指向不同的属性 - 同样的问题

  • 使用DOM定位器 selenium.GetAttribute(document.getElementsByTagName('A')[0] .getAttribute (HREF')) - 同样的问题;略有不同的错误信息(和错误信息丢失的最后括号)的。ERROR:元素document.getElementsByTagName('A')[0] .getAttribute('href属性找不到的注意,这种精确表达式正常工作在Firebug的控制台。

  • 使用绝对而不是相对的XPath寻址,用 selenium.GetText(XPath的= / HTML /体/ A [@ HREF])来确认是否存在,然后 selenium.GetAttribute(XPath的= / HTML /体/ A / @ HREF)来获得属性 - 和它的工作

  • 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 then selenium.GetAttribute("xpath=/html/body/a/@href") to get the attribute -- and it worked!

虽然手册明确指出的相对 XPath定位不需要!一个明确的定位器类型(即的XPath =前缀),它是沉默的绝对 XPath定位,我从这个解释,前缀是必需的,但出于好奇,我回到我的相对表达。并增加了明确的前缀 - 改变 selenium.GetAttribute(//一/ @ HREF) selenium.GetAttribute(XPath的= // A / @ HREF)! - 和这个还曾

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!

最后,我与实验非常方便的查找硒IDE按钮,表明它具有优良元素,但失败的属性。我能理解,这是没有意义的,因为属性是不可见的页面元素突出一个属性,但为什么不突出显示包含属性的元素,使之以不同的颜色?也许不是一个简单的任务...

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...

我蒸馏水上述实验的结果下来对这些问题的;这是我在这里张贴的全部目的!每一种似乎是给了我一个错误,但让我知道如果你觉得我的使用不正确或有解决方法:

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:


  1. 为什么的getAttribute使用XPath定位器类型唯一需要的明确的定位器类型,当其他方法(如的getText)不?

  2. 为什么DOM定位失败,并不发现'的错误? (该手册还明确指出,DOM定位器做的的需要一个明确的定位器类型前缀,但无论如何我尝试添加DOM =作为对DOM测试的前缀;它还是失败)

  3. 为什么硒IDE不试图突出(查找)的属性时失败更优雅?在相同的的XPath = //一/ @ HREF定位器,按下查找按钮收益率这个丑陋的消息:找不到[错误]定位:XPath的= //一/ @ HREF错误= [异常...无法转换的JavaScript参数arg 0 inIFlasher.scrollElementIntoView]nsresult:0x80570009(NS_ERROR_XPC_BAD_CONVERT_JS)的位置:JS框架::铬://硒-IDE /内容/硒runner.js :: showElement ::行386的数据:无]

  1. Why does GetAttribute with an XPath locator type uniquely require an explicit locator type when other methods (e.g. GetText) do not?
  2. 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.)
  3. 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]"

您输入还要求为以下几点:在这里我想对每个测试的模式是(A)的getText 定位器换元素与属性的)确认元素,则(B)的存在< STRONG>的getAttribute (定位器换属性本身的)。在表中的6个插槽下面我成功地解决了其中的3个像刚才详细介绍,第4似乎是一个错误。 为其余两个插槽有解决方案?

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')



(详细信息:硒RC版本1.0.3,浏览器:Firefox 3.6.13,在C#我的目标代码)

(Details: Selenium RC version 1.0.3, Browser: Firefox 3.6.13, My target code in C#)

推荐答案

硒区局的的getAttribute 方法返回element\attribute定位器的。这些定位器的一般形式是

Selenium RC's GetAttribute method returns the value of the element\attribute locator. The general form for these locators is

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

例如:

"SaveButton@href"

返回的值 id为元素的HREF 属性 SaveButton 。 XPath定位也可以用:

returns the value of the href attribute on the element with the id SaveButton. Xpath locators can also be used:

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

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

returns the value of the href attribute on the element whose id contains the text SaveButton.

要回答你的问题,


  • 1:我不吨真的知道,这是硒的设计者问题

  • 1: I don't really know, this is a question for the designers of Selenium.

2:硒命令执行多个不同的上下文。在一些命令,文件指的是网页的测试,在其他命令,文件指的是页面含有硒框架(testRunner.html我相信)

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:该错误消息说,它无法找到您所请求的元素。如果确实有一个bug存在,但并没有真正影响到你之后的信息可能是硒的团队很有用。的信息越多就越好,对吧?

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?

这篇关于硒的getAttribute方法并发症的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆