【问题标题】:selenium2.0 webelement cannot get Attribute of the htmlselenium2.0 webelement无法获取html的属性
【发布时间】:2012-05-14 02:20:09
【问题描述】:

我使用了pagefactory模式,在我的bean文件中,我使用xpath声明了WebElement:

@FindBy(xpath ='//div[5]/div/div/dl/dd[4]')
def public WebElement nextPage //nextpage

在我的工厂文件中(这个类扩展了 bean 类),我使用了

nextPage.getAttribute("class") 

但结果返回 null 或空。我不知道为什么...我只是想获取以下html的类,来判断这是一个可点击的链接还是一个普通的文本。

这里是html:

<a class="easyquery_paginglink" href='javascript:gotoPage("consumeRecord","consumeRecord",2)'>nextpage</a>

【问题讨论】:

  • 这就是我认为您要问的问题:我正在使用 Selenium WebDriver 选择页面上带有 XPath 的元素并希望获取其类属性。 这是正确的?如果是这样,您可以发布您收到的错误消息、您尝试获取的元素的 html 源代码以及您当前的 xpath 代码。
  • 是的,这就是问题所在。没有错误消息显示,当我使用 getAttribute("class") 时只返回 null 或空结果...
  • 这是html下一页

标签: xpath selenium getattribute


【解决方案1】:

您的 XPath 可以是 "//a[Text() = 'nextpage']" 然后使用 .getAttribute("class"); 所以:

IWebElement element = _driver.FindElement(By.XPath("//a[Text() = 'nextpage']"));
string className = element.GetAttribute("class");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多