Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 硒是否有可能通过其“标签”找到标签元素;至于;属性是否使用xpath?_Selenium_Xpath_Attributes_Qa - Fatal编程技术网

Selenium 硒是否有可能通过其“标签”找到标签元素;至于;属性是否使用xpath?

Selenium 硒是否有可能通过其“标签”找到标签元素;至于;属性是否使用xpath?,selenium,xpath,attributes,qa,Selenium,Xpath,Attributes,Qa,selenium是否可以使用xpath通过其“for”属性找到标签元素 我正在测试一个有7个“是”或“否”问题的站点。CSS的工作方式是,单击标签而不是输入单选按钮。通常我只是通过文本内容找到元素,但每个问题都有一个是和否 我想一个解决办法是用javascript点击输入按钮,绕过我尝试点击时遇到的“元素不可见”错误。直接点击输入,但我想看看是否有更好的方法来定位用户实际点击的标签 *为所有记不起标签元素外观的用户编辑。您将如何从这个组中选择中间的一个 <label for="superL

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

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

我想一个解决办法是用javascript点击输入按钮,绕过我尝试点击时遇到的“元素不可见”错误。直接点击输入,但我想看看是否有更好的方法来定位用户实际点击的标签

*为所有记不起标签元素外观的用户编辑。您将如何从这个组中选择中间的一个

<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>
  • 问题1 对 不
  • 问题2 对 不

我想,您正在寻找的XPath选项应该是“@”。因此,在您的情况下,类似于[…]/label/@for的内容将返回该for值,然后您可以使用该值检查您的位置;)

干杯
D

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

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

使用


它是。事实上我确实用过。但是你需要显示你正在谈论的元素的html,这是绝对不可能的,因为我们可以猜测你的应用程序是什么样子。通读并发布更多信息。为你们添加了一些代码。
label = self.browser.find_element_by_xpath('//label[@for="id_field"]')