【问题标题】:Is it possible for selenium to find a label element by its "for" attribute using xpath?selenium 是否可以使用 xpath 通过其“for”属性找到标签元素?
【发布时间】:2015-07-14 22:05:04
【问题描述】:

selenium 是否可以使用 xpath 通过其“for”属性找到标签元素?

我正在测试一个包含 7 个是或否问题的网站。 CSS 的完成方式是单击标签而不是输入单选按钮。通常我只是通过文本内容找到元素,但每个问题都有一个是和否。

我认为一种解决方法是在输入按钮上单击 javascript 以解决我在尝试直接 .click() 输入时遇到的“元素不可见”错误,但我想看看是否有更好的方法来定位用户实际点击的标签。

*为所有不记得标签元素是什么样子的人编辑。您将如何从该组中选择中间的那个?

<label for="superLongSetOfRandomCharacters123" class="common-to-all-buttons"> No </label>
<label for="superLongSetOfRandomCharacters345" class="common-to-all-buttons"> No </label>
<label for="superLongSetOfRandomCharacters456" class="common-to-all-buttons"> No </label>

更具体地说,这就是它们在 DOM 中的位置

<ul class="list-view list-view-dividers">
   <li class="list-view-item">
      <div class="content">
         <div id="variableValue1" class="multipleChoice">
            <div class="form-row">
               <label>Question 1</label>
               <div class="form-row">
                  <table class="choices" id="randomlyGenerated-1">
                     <tbody>
                        <tr>
                           <td>
                              <div class="ui-radio"><label for="randomlyGenerated1" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> Yes</label><input id="randomlyGenerated1" type="radio" name="randomlyGenerated1" value="Yes"></div>
                           </td>
                           <td>
                              <div class="ui-radio"><label for="randomlyGenerated2" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> No</label><input id="randomlyGenerated2" type="radio" name="randomlyGenerated2" value="No"></div>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
            <div id="randomlyGenerated1-Error" class="component-error"></div>
         </div>
      </div>
   </li>
   <li class="list-view-item">
      <div class="content">
         <div id="variableValue2" class="multipleChoice">
            <div class="form-row">
               <label>Question 2</label>
               <div class="form-row">
                  <table class="choices" id="randomlyGenerated-2">
                     <tbody>
                        <tr>
                           <td>
                              <div class="ui-radio"><label for="randomlyGenerated21" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> Yes</label><input id="randomlyGenerated21" type="radio" name="randomlyGenerated21" value="Yes"></div>
                           </td>
                           <td>
                              <div class="ui-radio"><label for="randomlyGenerated22" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> No</label><input id="randomlyGenerated22" type="radio" name="randomlyGenerated22" value="No"></div>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
            <div id="randomlyGenerated2-Error" class="component-error"></div>
         </div>
      </div>
   </li>
</ul>

【问题讨论】:

  • 是的。事实上,我确实使用过它们。但是你需要显示你正在谈论的元素的html
  • 绝对不可能的一件事是让我们能够猜测您的应用是什么样的。阅读How to Ask 并发布更多信息。
  • 为你们添加了一些代码。

标签: selenium xpath attributes qa


【解决方案1】:

它适用于选择目标为“for”的所有标签: label[@for='superLongSetOfRandomCharacters123']

或者您可以将@for=''留空以选择具有任何值的所有标签(即label[@for]

【讨论】:

    【解决方案2】:

    我猜,您正在寻找的 XPath-Option 将是“@”。所以在你的情况下,像 [...]/label/@for 这样的东西会返回你那个 for-value,然后你可以用它来检查你在哪里;)

    干杯 D

    【讨论】:

      【解决方案3】:

      使用find_element_by_xpath

      label = self.browser.find_element_by_xpath('//label[@for="id_field"]')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 2021-03-20
        • 2014-08-19
        • 1970-01-01
        • 2022-07-05
        • 1970-01-01
        • 2016-03-12
        相关资源
        最近更新 更多