Home:ALL Converter>How to identify the checkbox using the xpath as per the HTML

How to identify the checkbox using the xpath as per the HTML

Ask Time:2018-06-08T21:58:46         Author:Chinmay235

Json Formatter

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

This is my form and I need to click the checkbox using the xpath. I have tried below methods.

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

and

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

System.out.println(s1);

both returns me false.Please help me how to check the checkbox.

Thank you in advance.

Author:Chinmay235,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762313/how-to-identify-the-checkbox-using-the-xpath-as-per-the-html
Andersson :

Try to use below XPath to fetch required checkbox:\n\n//div[.='I agree to Muvi’s Term of Service']//input\n\n\nYou can also try to locate it by @name:\n\ndriver.findElement(By.name(\"terms\"));\n\n\nAlso note that in your second XPath this part //*form is syntactically incorrect. You should specify either tag name //form to fetch form node only or wild-card //* to fetch node with any name, but not both at the same time",
2018-06-08T14:05:34
undetected Selenium :

To invoke click() on the checkbox with text associated as Term of Service you can use either of the following Locator Strategies:\n\n\nXPath:\n\n\"//label[@class='checkbox checkbox-inline']/input[@name='terms']\"\n\nCSS:\n\n\"label.checkbox.checkbox-inline > input[name=terms]\"\n\n",
2018-06-08T15:07:30
yy