【问题标题】:Java and Selenium - select radio button in list by textJava 和 Selenium - 按文本在列表中选择单选按钮
【发布时间】:2019-11-07 13:14:02
【问题描述】:

我正在尝试按文本选择列表中的条目。 li 条目也有 id,但我想改用文本。

我有以下html sn-p:

<fieldset felt id="bosted"
  <ul class="feltliste ng-scope">
    <li> 
      <label id="lblbostedleierKommunalt" for="bostedleierKommunalt" class="label ">
         Leier kommunal bolig
      </label>
    </li>(verdi, tekst) in bostedTyper -->
    <li>
      <label id="lblbostedleierPrivat" for="bostedleierPrivat" class="label ">
         Leier bolig
        <div class="hjelpetekst ng-scope ng-isolate-scope" ng-class="{'hjelpetekst--utvidet': visHjelpetekst}" id="leierPrivat-hjelpetekst " ng-if="verdi === 'LEIER_PRIVAT'">
          <button title="Vis hjelpetekst" type="button" ng-click="visEllerSkjulHjelpetekst($event)" aria-describedby="leierPrivat-hjelpetekst ">
          <span class="bare-skjermleser" translate=""><span class="ng-scope">Vis hjelpetekst</span></span>
          <est-ikon type="sporsmalstegn"><svg focusable="false"><use xlink:href="#ikon-sporsmalstegn"></use></svg>
        </div>

上例中的文本条目是

Leier kommunal bolig
雷尔博利格

选项 1: 我正在尝试这样做:

driver.findElement(By.cssSelector("id='bosted' li[text=" + text + "]")).click();

我尝试过“价值”和其他选项。但我不断收到“指定了无效或非法的选择器”。

选项 2:我也尝试过此解决方案,取自此处类似问题的答案:

List<WebElement> options = bosted.findElements(By.tagName("li"));
    for (WebElement option : options) {
        if (option.getText().equals(bosituasjon)) {
                option.click();
                break;
        }
    }

除了我觉得它看起来很凌乱而且不是很优雅之外,它也不起作用。在观看测试运行时,它似乎试图单击页面上目标条目上方的条目。 (虽然我不确定。)不过,它不会返回任何错误。

但是,它确实找到了所有文本条目,并且 if 循环命中并执行 option.click() 行。

但显然有问题,因为它没有导航到页面上的列表条目,并且没有选择该选项。

另外,如果我使用 .sendkeys(Keys.ENTER) 而不是 .click(),我会收到“元素不可交互”错误。

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    使用下面自定义的 xpath,它几乎和使用 id 或类名一样快。

    driver.findElement(By.xpath("//label[contains(text(),'Leier kommunal bolig')]")).click();
    

    【讨论】:

    • 这会导致“无法定位元素:{”method”:”xpath”,”selector”:”//fieldset[@id = 'bosted']/descendant::li[text( ) = 'Leier kommunal bolig']"}"
    • 你能打开开发者工具并用 //label[contains(text(),'Leier kommunal bolig')] 搜索你看到的结果是 1/1,那么上面的代码应该可以工作。
    【解决方案2】:

    您应该在选择器中使用value,而不是text

    driver.findElement(By.cssSelector("id='bosted' li[value=" + text + "]")).click();
    

    不管怎样,你可以使用XPath选择器,里面使用id。

    driver.findElement(By.xpath("//fieldset[@id = 'bosted']/descendant::li[text() = '" + text + "']")).click();
    

    【讨论】:

    • 我真的不喜欢 XPath,因为它不像 id 标签那样是页面的稳定部分。
    • 更新答案,试试价值
    • 我的错。价值是我尝试的第一件事。文本是下一个。值导致相同的错误(无效或非法的选择器)。该条目没有 value 属性;父标签有一个,但由于我需要使用文本,所以该值不适用。
    • xpath方法导致:"这导致"无法定位元素:{"method":"xpath","selector":"//label[contains(text(),'Leier kommunal bolig')]"}
    • 嘿,我的方法不一样,见上文。
    【解决方案3】:

    CSS 选择器不允许通过内部文本选择元素。请改用 XPath 选择器。

    P.S. - 如果您仍然需要使用第二个选项,您应该在比较之前修剪文本。喜欢option.getText().trim().equals(bosituasjon),因为 html 中的文本可能采用复杂的形式..

    P.P.S. - 如果您认为您的第二个选项仍然不够优雅,您可以应用 steams api 并使您的代码如下所示:

       List<WebElement> options = bosted.findElements(By.tagName("li"));
       options.stream().filter(i -> i.getText().trim().equals(bosituasjon)).findFirst().get().click();
    

    【讨论】:

    • 但问题是选项是匹配的 - option.getText().equals(bosituasjon) 是真的。只是点击没有执行。使用 stream() 时也是如此。
    • 哦,我明白了。然后试试这个List&lt;WebElement&gt; options = bosted.findElements(By.xpath("//li/label"));。如果元素不处理您的点击,那么它要么没有关联的处理程序。所以我会尝试点击下一级或上一级(在你的情况下看起来没有意义)
    • List options = bosted.findElements(By.xpath("//li/label"));顺便说一句,导致流“没有值存在”。
    • 这个页面是公开的吗?
    • 很遗憾,没有。
    【解决方案4】:

    没有使用 css 选择器的文本选项。您应该改用 Xpath。

    driver.findElement(By.xpath("//*[@id='bosted']//li[contains(.,'" + text + "')]")).click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-14
      • 2012-07-30
      • 2014-06-18
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多