首页 > 解决方案 > 如何根据 HTML 使用 xpath 识别复选框

问题描述

<html class="no-js home_old_responsive_test" prefix="og: http://ogp.me/ns#" lang="en-US">
   <body class="page-template page-template-page-templates page-template-custom-signup page-template-page-templatescustom-signup-php page page-id-29873 page-parent do-etfw muvi-child" data-spy="scroll" data-target="#main-navbar" id="29873">
      <div class="container freetrail-container">
         <div class="row frt_content_box">
            <div class="col-md-6 col-sm-6 frt_form frt_rht_box">
               <form method="post" name="userinfo" id="userinfo" novalidate="novalidate">
                  <div class="form-group fg-float">
                     <div class="fg-line">
                        <input class="input-sm form-control fg-input" name="name" id="name" value="" type="text">
                        <label class="fg-label">Name</label>
                     </div>
                     <label id="name-error" class="error" for="name"></label>
                  </div>
                  <div class="form-group fg-float">
                     <div class="fg-line">
                        <input class="input-sm form-control fg-input" name="companyname" id="companyname" value="" onblur="autofill_domain()" type="text">
                        <label class="fg-label">Company Name</label>
                     </div>
                     <label id="companyname-error" class="error" for="companyname"></label>
                  </div>
                  <div class="form-group fg-float">
                     <div class="fg-line">
                        <input class="input-sm form-control fg-input" name="phone" value="" type="text">
                        <label class="fg-label">Phone Number (with country code) </label>
                     </div>
                     <label id="phone-error" class="error" for="phone"></label>
                  </div>
                  <div class="form-group fg-float">
                     <div class="fg-line">
                        <input class="input-sm form-control fg-input inputchk" id="email" name="email" value="" type="text">
                        <label class="fg-label">Email</label>
                     </div>
                     <label id="email-error" class="error" for="email"></label>
                  </div>
                  <div class="form-group fg-float">
                     <div class="fg-line">
                        <input class="input-sm form-control fg-input" id="inputPassword" name="password" value="" type="password">
                        <label class="fg-label">Password</label>
                     </div>
                     <label id="inputPassword-error" class="error" for="inputPassword"></label>
                  </div>
                  <div class="form-group fg-float">
                     <div class="fg-line">
                        <input class="input-sm form-control fg-input" placeholder="yourdomainname." id="subdomain" name="subdomain" onkeyup="getdomainUrl();" onblur="getdomainUrl();" autocomplete="off" value="" style="padding-top:10px;" type="text">
                        <label class="fg-label fg-label-domain">Domain Name</label>
                     </div>
                     <label id="subdomain-error" class="error" for="subdomain"></label>
                     <label class="hintlbl2">Your website will be at <span class="domain-url">http://www.<span class="disabled subText" id="subText">yourname</span>.edocent.com</span>, you can change this later.</label>
                  </div>
                  <input name="studio_email" id="studio_email" value="" style="display: none;" type="text">
                  <div class="text-center">
                     <div><label class="checkbox checkbox-inline">
                        <input name="terms" type="checkbox">
                        <i class="input-helper"></i>
                        I agree to Muvi’s <a href="/agreements/muvi-terms-service" target="_blank" class="termservice">Term of Service</a>
                        </label>
                     </div>
                     <label id="terms-error" class="error" for="terms"></label>
                  </div>
                  <div class="form-group fg-float nxtbtnbox">
                     <input name="country" value="" readonly="readonly" type="hidden">
                     <input name="region" value="" readonly="readonly" type="hidden">
                     <input value="0" id="is_submit" name="is_submit" type="hidden">
                     <input value="" name="purchasetype" type="hidden">
                     <input value="" id="studio_id" type="hidden">
                     <button type="submit" id="nextbtn" name="nextbtn" class="btn btn-primary frt_btn">NEXT</button>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </body>
</html>

这是我的表单,我需要使用 xpath 来单击复选框。我尝试了以下方法。

boolean s1 = driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[1]/form/div[7]/div/label/input")).isDisplayed();

boolean s1 = driver.findElement(By.xpath("//*form[@id='userinfo' and @name='userinfo']/div[7]/div/label/input[@name='terms']")).isDisplayed();

System.out.println(s1);

两者都返回错误。请帮助我如何检查复选框。

先感谢您。

标签: seleniumselenium-webdriverxpathcss-selectorswebdriver

解决方案


尝试使用下面的 XPath 来获取所需的复选框:

//div[.='I agree to Muvi’s Term of Service']//input

您也可以尝试通过以下方式找到它@name

driver.findElement(By.name("terms"));

另请注意,在您的第二个 XPath 中,这部分//*form在语法上是不正确的。您应该指定标签名称//formform仅获取节点或通配符//*以获取具有任何名称的节点,但不能同时指定两者


推荐阅读