【问题标题】:How to grab text corresponding to checkboxes in selenium webdriver?如何获取与 selenium webdriver 中的复选框相对应的文本?
【发布时间】:2016-02-29 04:41:58
【问题描述】:

我正在使用 selenium webdriver 来运行脚本。

我有一个场景,我想抓取对应于复选框的文本。

对于单个复选框,我使用 getAttribute() 来捕获下面代码中给出的文本,它工作正常。

String referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']//input")).getAttribute("id"); 

在 findElements() 的情况下 getAttribute 是否无效?

如何捕获多个复选框的文本?

复选框的屏幕截图:

HTML 截图:

正如您在屏幕截图中看到的,我希望属性 - id 来捕获我的文本。

【问题讨论】:

    标签: java selenium checkbox selenium-webdriver


    【解决方案1】:

    只是从您那里获取另一个question 的代码并对其进行修改。

    String referenceIn3DPage =null;
    int count=Driver.driver.findElements(By.xpath("//div[3][@class='viewer3d-demo-commercial-references-checkboxes']//input")).size();
    System.out.println("the count="+count);
    
    for(int i=1;i<=count;i++)
    {
      referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='viewer3d-demo-commercial-references-checkboxes']/div["+i+"]/label/input")).getAttribute("id"); 
      System.out.println("the value in 3d= "+referenceIn3DPage);
    }
    

    请运行上面的代码,如果它给你预期的结果,请告诉我。

    【讨论】:

    • 非常感谢@Manu。代码工作正常。我在评估 xpath 时犯了错误。
    • 欢迎您!是的,你做得很好,只是一个错字:)
    【解决方案2】:

    请使用 value 参数获取复选框文本:

    String referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']//input")).getAttribute("value");
    

    输出: OSCP120 [如果给定的 xpath 是第一个复选框]

    【讨论】:

    • 我想要与所有复选框对应的文本列表,而不是特定复选框的列表。我想要 OSCP120、PCFD030 等值。
    • 然后您将使用 List 而不是单个字符串进行迭代。
    • 能否详细说明一下?
    • 使用 size(),我找到了复选框的计数。现在,问题是如何将 referenceIn3DPage 从 1 迭代到 9? int count=Driver.driver.findElements(By.xpath("//div[3][@class='some-class']//input")).size(); System.out.println("the count="+count); for(int i=1;i&lt;=count;i++) { referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']//input")).getAttribute("id"); System.out.println("the value in 3d= "+referenceIn3DPage); }
    • 计数返回 9。
    【解决方案3】:

    你可以试试:.getAttribute("value");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-25
      • 2019-03-15
      • 2019-03-24
      • 1970-01-01
      • 2012-08-24
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多