【问题标题】:Finding a specific attribute, Selenium Xpath Java查找特定属性,Selenium Xpath Java
【发布时间】:2016-01-04 12:54:47
【问题描述】:

我有下面的代码,我试图从(data-id)中提取特定属性。 我是使用 selenium 的新手,已经为此痛苦了一天多。

为了添加上下文,我将向您介绍我正在努力实现的目标。

我有一个包含拍卖的网页,拍卖有一个 ID,拍卖中的所有物品都有唯一的 ID,但都链接到原始拍卖 ID。 我正在尝试提取元素的“data-id”属性,但是我还没有弄清楚如何。 这是我试图从中获取 id 的代码的 sn-p。

<div class="dropdown open">
  <a class="dropdown-toggle form-control" href="#" data-toggle="dropdown">
  <ul class="dropdown-menu dropdown-menu-form" role="menu">
    <li id="liAuction4c42556772376a443736343d">
      <label class="checkbox">
        <input id="chkAuction4c42556772376a443736343d" class="auction" type="checkbox" data-caption="09-10-2015 10:30:00" data-id="4c42556772376a443736343d" checked="checked"/>
09-10-2015 10:30:00
      </label>
    </li>
  </ul>
</div>

我在很多论坛上搜索了整个谷歌,但没有找到适合我的解决方案,否则我不会发布问题,看起来像一个完整的新手。

我曾尝试使用 .getAttribute,但是我遇到了一些问题,并且代码从未编译过,我想我没有正确地做某事。

String dataID = selenium.findElement(By.xpath("//*[starts-with(@id, 'liAuction')]")).getAttribute("data-id");

当我尝试上述操作时,“findElement”部分带有红色下划线,并且我收到以下消息,

“未定义 Selenium 类型的 findElement(By) 方法”。

如果我改变父母身份看起来像这样;

String dataID = selenium.findElement(By.xpath("//*[starts-with(@id, 'liAuction')]").getAttribute("data-id"));

“findElement”不再带下划线,但是现在“.getAttribute”部分带有红色下划线,并且我收到以下消息,“方法 getAttribute(String) 未定义 By 类型”

在我即将把笔记本电脑扔出窗外时,我非常感谢您的帮助,而且我真的不想丢失所有文件。

提前致谢

托尼

【问题讨论】:

    标签: java selenium xpath undefined getattribute


    【解决方案1】:

    您可以使用getAttribute 方法。

    先找到输入元素,然后提取data-id:

    WebElement inputElement = selenium.findElement(By.id("chkAuction4c42556772376a443736343d"));
    String data-id = inputElement.getAttribute("data-id");
    

    【讨论】:

    • 您好,感谢您的快速回复,我尝试了您的建议,但“驱动程序”带有红色下划线,并要求为其声明一个变量。我假设我应该用“硒”代替它是否正确?当我更换它时,我遇到了与原始帖子中描述的相同的问题
    • 对不起,如果这是一个基本问题,但我是新手,还没有任何想法
    • 哈哈,对不起,这就是你的代码中所谓的“硒”!,会更新我的答案;-)
    【解决方案2】:

    在xpath下面使用:-

    //input[@id='chkAuction4c42556772376a443736343d']/@data-id
    

    然后使用:-

    String dataID = selenium.findElement(By.xpath("//input[@id='chkAuction4c42556772376a443736343d']/@data-id")).gettext();
    

    完整代码:-

    static WebDriver driver=null;
        public static void main(String[] args) {
             driver = new FirefoxDriver();
             driver.get("URL");
             String dataID = driver.findElement(By.xpath("//input[@id='chkAuction4c42556772376a443736343d']/@data-id")).gettext();
             System.out.println(dataID);
    }
    

    希望对你有帮助:)

    【讨论】:

    • 您好,谢谢您,我可以看到这应该如何工作,但是我仍然遇到同样的问题,“findElement”带有红色下划线,工具提示消息是“方法 findElement(By) 未定义硒类型”。这一直是我的头疼
    • 我已经更新了我的答案,你可以试试完整的代码。在它之间添加您需要的代码应该可以工作
    • 谢谢你,这不是完全正确的答案,而是指出我想要的正是我想要的我在 data-id 属性中使用了以下内容。你的回答比你知道的更有帮助,再次感谢 String dataID = selenium.getValue("//input[@id='chkAuction4c42556772376a443736343d']/@data-id");
    【解决方案3】:

    感谢您对这些人的关心,没想到会这么快得到答案。

    我需要使用以下行来检索元素的“data-id”属性。

    String dataID = selenium.getValue("//input[@id='chkAuction4c42556772376a443736343d']/@data-id");
    

    这最终比最初想象的要容易得多,我要感谢@Shubham Jain 的帮助,他的建议指出了我需要去的地方。

    我希望这对将来的其他人有所帮助

    【讨论】:

      【解决方案4】:

      您好,我建议您使用以下代码。它会工作得很好。

      WebElement element = selenium.findElement(By.xpath("//input[@class='auction']"));
      String dataId= element..getAttribute("data-id");
      

      详情

      我正在使用以下 xpath //input[@class='auction'] 识别对象并将其保存到 WebElement 变量中。

      然后通过使用getAttribute() 我从data-id 获取字符串

      【讨论】:

        猜你喜欢
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        相关资源
        最近更新 更多