Csharp/C#教程:Selenium的GetAttribute方法的并发症分享


Selenium的GetAttribute方法的并发症

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

然后我尝试了:

虽然本手册明确指出相对 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属性的值。

要回答你的问题,

https://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")就像你已经成功尝试过一样。

上述就是C#学习教程:Selenium的GetAttribute方法的并发症分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1039658.html

(0)
上一篇 2022年1月27日
下一篇 2022年1月27日

精彩推荐