【问题标题】:Selenium Webdriver capture Checkbox textSelenium Webdriver 捕获复选框文本
【发布时间】:2018-09-07 21:13:09
【问题描述】:

您好,我正在尝试捕获位于同一跨度类中的复选框旁边的名称,但该名称位于标签标签中。尝试使用 getText() 方法捕获文本时,我得到的是空名称。

在代码中显示它我是怎么做的。

//to find the checkbox
@FindBy(how = How.ID, id = "ConstructionManagement")
private WebElement constructionMgmnt;

当使用这个 getText 但我得到的是空文本。使用 getAttribute 时,我得到的复选框的实际名称是 $PpyWorkPage$pOutageRequest$pConstructionManagement

constructionMgmnt.getText();
constructionMgmnt.getAttribute("name")

复选框是如何编码的页面源代码。

<span class="checkbox" data-ctl="Checkbox">
   <input value="false" name="$PpyWorkPage$pOutageRequest$pConstructionManagement" type="hidden">
   <input id="ConstructionManagement" class="checkbox chkBxCtl" value="true" name="$PpyWorkPage$pOutageRequest$pConstructionManagement" validationtype="true-false" pn=".ConstructionManagement" type="checkbox">
   <label class=" cb_standard" for="ConstructionManagement">Construction Management</label>
</span>

谁能想到如何从指向复选框 ID 的元素中获取文本,或者我必须使用 xpath 来标记以获取该复选框旁边的文本?

//example of garbing the name of the Label (I tested this and it works)
driver.findElement(By.xpath("//label[@for='ConstructionManagement']"));

谢谢。

【问题讨论】:

    标签: selenium selenium-webdriver checkbox


    【解决方案1】:

    使用以下代码定位标签:

    //to find the Label Text
    @FindBy(how = How.ID, xpath = "//input[@id='ConstructionManagement')//following::label[1]"
    private WebElement constructionMgmnt_label;
    

    接下来您可以使用以下方法检索复选框文本,如下所示:

    String my_label = constructionMgmnt_label.getAttribute("innerHTML")
    

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2017-06-09
      • 2017-11-10
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多