如何根据HTML使用xpath标识复选框

时间:2018-06-08 13:58:47

标签: selenium selenium-webdriver xpath css-selectors webdriver

<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);

两者都返回false。请帮我查看复选框。

提前谢谢。

3 个答案:

答案 0 :(得分:1)

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

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

您还可以尝试按@name找到它:

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

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

答案 1 :(得分:0)

要在包含服务条款相关文字的复选框上调用"//label[@class='checkbox checkbox-inline']/input[@name='terms']" ,您可以使用以下任一Locator Strategies

  • 的XPath

    "label.checkbox.checkbox-inline > input[name=terms]"
    
  • CSS

    Evaluate

答案 2 :(得分:0)

您可以在下面使用

  1. Xpath的

    ".//*[@id='userinfo']/div[7]/div/label/input"
    
    1. CSS选择器

      .checkbox > input:nth-child(1)