布尔检查后单击单选按钮

时间:2018-01-05 09:42:40

标签: java selenium selenium-webdriver

我正在检查单选按钮。

我找到了页面上的所有按钮,如果它们是假的,我们想点击它们。

我实际上很难点击它...如果结果是假的。

我怎么点击它?

----------------------------------------------- ------------------------------

我已附上我当前的代码和页面html。

List<WebElement> elementxb1 = driver.findElements(By.xpath("//input[@name='contractPeriod']"));

    for(int i11 = 0 ; i11< elementxb1.size() ; i11++){ 

          boolean vapval2b = elementxb1.get(i11).findElement(By.xpath("//input[@name='contractPeriod']")).isSelected();

            if(vapval2b)
            {
                    System.out.println("Yes Button is selected ? : " + vapval2b); 
            }
            else 
            {
                System.out.println("No Button is not selected, click on it. ? :" + vapval2b); 
                // click on the radio button here.
            }
       }

HTML:

----------------------------------------------- ------------------------------

<html ng-app="dan" class="ng-scope"><head><style type="text/css">[uib-typeahead-popup].c-dropdown-menu{display:block;visibility:visible;opacity:1;}</style><style type="text/css">@charset "UTF-8";[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak,.ng-hide:not(.ng-hide-animate){display:none !important;}ng\:form{display:block;}.ng-animate-shim{visibility:hidden;}.ng-anchor{position:absolute;}</style>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
  <base href="/">

  <title></title>
  <link rel="shortcut icon" href="favicon.ico">
  <link rel="stylesheet" href="/build/styles.css?v=0.1">

  <!-- Prefetch DNS for all other external assets -->
  <link rel="dns-prefetch" href="//ajax.googleapis.com">
</head>
<body class="u-scroll-y ng-scope" ng-controller="CoreController as cc" style="">

  <!-- Begin Alerts -->
  <div class="c-animate  c-animate--show  u-pos-f-t ng-hide" ng-show="global.alerts.length">
    <div class="o-grid">
      <div class="o-grid__row">
        <div class="o-grid__col-lg-8 o-grid__col-lg-offset-2  u-pt-md">
          <!-- ngRepeat: alert in global.alerts -->
        </div>
      </div>
    </div>
  </div>
  <!-- End Alerts -->

  <div class="o-grid-fluid  u-h-100  u-pl-0"> 
    <div class="o-grid__row  u-ml-0  u-h-100">
      <div class="c-loader ng-hide" ng-show="loadingHome">
        <div class="c-loader-spinner  u-center-block"></div>
      </div>

      <!-- ngIf: global.loggedIn --><nav class="o-grid__col-xl-2  o-grid__col-lg-3  o-grid__col-xs-4 u-p-0  c-card__block  u-h-100  u-shadowed  u-pos-f-t ng-scope" ng-if="global.loggedIn" style="">

        <div class="o-layout-table  u-h-100">
          <div class="o-layout-table__row">
            <div class="u-p  u-bb">
              <a href="#/home">
                <img src="/images/cars-logo-landscape.svg" alt="Car logo">
              </a>
            </div>
          </div>

          <div class="o-list-group  u-text-90">

            <a href="#/home" class="o-list-group__item  u-pl-md u-py-sm  u-text-80 u-text-capitalize" ng-class="{ 'u-text-primary u-bg-faded  u-text-primary': cc.isActive('/home') }" style="">
              <div class="u-py-sm">
                <i class="i-icon  i-icon--angle-right  u-text-100  u-pull-right"></i>Home
              </div>
            </a>

            <a href="#/starting-questions" class="o-list-group__item  u-pl-md u-py-sm  u-text-80 u-text-capitalize" ng-class="{ 'u-text-primary u-bg-faded  u-text-primary': cc.isActive('/starting-questions'), 'u-disabled' : !cc.answers() }" style="">
              <div class="u-py-sm">
                <i class="i-icon i-icon--angle-right  u-text-100  u-pull-right"></i>Starting Questions
              </div>
            </a>

            <a href="#/recommendations" class="o-list-group__item  u-pl-md u-py-sm  u-text-80 u-text-capitalize" ng-class="{ 'u-text-primary u-bg-faded  u-text-primary': cc.isActive('/recommendations'), 'u-disabled' : !cc.questions() }" style="">
              <div class="u-py-sm">
                <i class="i-icon  i-icon--angle-right  u-text-100  u-pull-right"></i>Recommendations
              </div>
            </a>

            <a href="#/purchase-decisions" class="o-list-group__item  u-pl-md u-py-sm  u-text-80 u-text-capitalize" ng-class="{ 'u-text-primary u-bg-faded  u-text-primary': cc.isActive('/purchase-decisions'), 'u-disabled' : !cc.recommend() }" style="">
              <div class="u-py-sm">
                <i class="i-icon  i-icon--angle-right  u-text-100  u-pull-right"></i>Purchase Decisions
              </div>
            </a>

            <a href="#/finance-contract-conclusion-form" class="o-list-group__item  u-pl-md u-py-sm  u-text-80 u-text-capitalize u-text-primary u-bg-faded" ng-class="{ 'u-text-primary u-bg-faded  u-text-primary': cc.isActive('/finance-contract-conclusion-form') }" style="">
              <div class="u-py-sm">
                <i class="i-icon  i-icon--angle-right  u-text-100  u-pull-right"></i>FCC Form
              </div>
            </a>

            <a href="#/finish" class="o-list-group__item  u-pl-md u-py-sm  u-text-80 u-text-capitalize" ng-class="{ 'u-text-primary u-bg-faded  u-text-primary': cc.isActive('/finish') }">
              <div class="u-py-sm">
                <i class="i-icon  i-icon--angle-right  u-text-100  u-pull-right"></i>Finish and sign
              </div>
            </a>

            <div class="o-list-group__item  u-w-100 u-bt  u-bg-faded  u-text-80  u-text-capitalize  u-pos-a-b  u-push-up-btn-lg  u-pl-md  u-pb-sm">
              <div class="u-py-sm u-bb u-mb-sm">
                  <i class="i-icon i-icon--file  u-text-100 u-pull-right"></i>Documents
              </div>
              <!-- ngIf: cc.forms.recommendVap --><div class="u-mb-sm  u-pointer ng-scope" ng-if="cc.forms.recommendVap" ng-click="cc.previewForms()" style=""><i class="i-icon i-icon--pdf  u-text-100 u-pr-sm"></i> VAP Recommendations</div><!-- end ngIf: cc.forms.recommendVap -->
              <!-- ngIf: cc.forms.recommendWarranty --><div class="u-mb-sm  u-pointer ng-scope" ng-if="cc.forms.recommendWarranty" ng-click="cc.previewForms()"><i class="i-icon i-icon--pdf  u-text-100 u-pr-sm"></i> Warranty Recommendations</div><!-- end ngIf: cc.forms.recommendWarranty -->
              <!-- ngIf: cc.forms.vapTerms --><div class="u-mb-sm  u-pointer ng-scope" ng-if="cc.forms.vapTerms" ng-click="cc.previewForms()"><i class="i-icon i-icon--pdf  u-text-100 u-pr-sm"></i>  VAP key facts leaflet</div><!-- end ngIf: cc.forms.vapTerms -->
              <!-- ngIf: cc.forms.purchaseVap --><div class="u-mb-sm  u-pointer ng-scope" ng-if="cc.forms.purchaseVap" ng-click="cc.previewForms()" style=""><i class="i-icon i-icon--pdf  u-text-100 u-pr-sm"></i> VAP Purchase Decisions</div><!-- end ngIf: cc.forms.purchaseVap -->
              <!-- ngIf: cc.forms.purchaseWarranty --><div class="u-mb-sm  u-pointer ng-scope" ng-if="cc.forms.purchaseWarranty" ng-click="cc.previewForms()"><i class="i-icon i-icon--pdf  u-text-100 u-pr-sm"></i> Warranty Purchase Decisions</div><!-- end ngIf: cc.forms.purchaseWarranty -->
              <!-- ngIf: cc.forms.optOut -->
              <!-- ngIf: cc.forms.fcc -->
            </div>

            <!-- Bottom -->
            <a class="o-list-group__item  u-bt  u-pos-a-b" ng-click="cc.optOut(); cc.utils.audit('opt_out')">
              <label class="c-option  u-px  u-py-sm  u-text-capitalize  u-clearfix  u-mb-0">
                <input type="checkbox" name="" value="" ng-model="optout" class="ng-pristine ng-untouched ng-valid">
                <i class="i-icon i-icon--close  u-text-100 u-pull-right"></i>opt out
              </label>
            </a>

            <!-- <a href="#/settings" class="o-list-group__item  u-bt  u-pos-a-b">
              <label class="c-option  u-px  u-py-sm  u-text-capitalize  u-clearfix  u-mb-0">
                <input type="checkbox" name="" value="{{ $index }}"
                ng-model="global.vap">
                <i class="i-icon i-icon--cog  u-text-100 u-pull-right"></i>Settings
              </label>
            </a> -->   
          </div>
        </div>
      </nav><!-- end ngIf: global.loggedIn -->

      <div class="u-p-0  u-h-100  o-grid__col-xl-10 o-grid__col-lg-9 o-grid__col-xs-8 u-pull-right" ng-class="{ 'o-grid__col-xl-10  o-grid__col-lg-9  o-grid__col-xs-8   u-pull-right' : global.loggedIn }" style="">

        <!-- ngIf: global.loggedIn --><header class="o-layout-table__row u-bg-primary u-shadowed u-clearfix u-px ng-scope" ng-if="global.loggedIn" style="">
          <div class="u-p-sm u-text-left u-pull-left ng-binding">
            <i class="i-icon i-icon--calendar"></i>&nbsp;&nbsp;Friday, January 5, 2018
          </div>
          <a href="#" class="c-btn c-btn--primary u-pull-right u-pull-right" ng-click="cc.signOut(); cc.utils.audit('sign_out')">
            <i class="i-icon i-icon--user-minus"></i>&nbsp;&nbsp;&nbsp;Sign out
          </a>
        </header><!-- end ngIf: global.loggedIn -->
        <main class="o-view-container">
          <div class="o-grid-fluid  u-py-md">
            <div class="o-grid__row">
              <!-- ngView:  --><div class="c-animate  c-animate--view-slide-in-right  c-animate--view-slide-out-right ng-scope" ng-view="" style=""><div class="o-grid__col-md-10 o-grid__col-xl-8 o-grid__col-xl-offset-2 o-grid__col-md-offset-1 ng-scope"><div class="u-mb-lg u-text-center"><h1 class="u-text-bold">Finance Contract Conclusion Form</h1></div><!-- Previously sent --><!-- ngIf: fcc.prevFcc() --><!-- Interest calculator and other info --><!-- ngIf: fcc.fccAlone && !fcc.prevFcc() --><!-- Form --><!-- ngIf: !fcc.prevFcc() --><div class="o-media c-card c-card__block u-shadowed u-mb-lg ng-scope" ng-if="!fcc.prevFcc()"><form name="fccForm" ng-submit="fccForm.$valid" class="ng-pristine ng-invalid ng-invalid-required"><div class="c-form-group"><label class="c-form-control-label u-p-0 u-w-100">Within 24 hour Pre Contract Review Period:</label><label class="c-option c-option--right u-px u-py-sm u-clearfix" ng-click="cc.utils.audit('fcc_24_hour_yes')"><input type="radio" required="" ng-model="fcc.values.contractPeriod" value="Yes" name="contractPeriod" class="ng-pristine ng-untouched ng-invalid ng-invalid-required"> <i class="c-option__radio"></i>Yes</label><label class="c-option c-option--right u-px u-py-sm u-clearfix" ng-click="cc.utils.audit('fcc_24_hour_no')"><input type="radio" required="" ng-model="fcc.values.contractPeriod" value="No" name="contractPeriod" class="ng-pristine ng-untouched ng-invalid ng-invalid-required"> <i class="c-option__radio"></i>No</label></div><div class="o-grid__col-md-6 u-mt"><input type="submit" class="c-btn c-btn--primary c-btn--block u-pull-right" value="Complete and Preview" ng-disabled="fccForm.$invalid" ng-click="fccForm.$valid &amp;&amp; fcc.previewPDF(fccForm.$valid); cc.utils.audit('fcc_complete')" disabled="disabled"></div><div class="o-grid__col-md-6 u-mt"><input type="submit" class="c-btn c-btn--primary c-btn--block u-pull-right" value="Skip" ng-click="fcc.skipPDF(); cc.utils.audit('fcc_skip')"></div></form></div><!-- end ngIf: !fcc.prevFcc() --></div></div>
            </div>
          </div>
        </main>
      </div>  
    </div>
  </div>

  <!-- Scripts -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
  <script src="./build/app.js?v=2.13" defer=""></script>

</body> <xpather id="xpather">      <form id="xpather-form">            <input id="xpather-xpath" type="text" placeholder="enter XPath…" autocomplete="off" spellcheck="false">     </form>         <xpather id="xpather-result"></xpather>     <xpather id="xpather-sidebar-toggler"></xpather>    </xpather>  <xpather id="xpather-sidebar">      <xpather id="xpather-sidebar-spacer"></xpather>     <xpather id="xpather-sidebar-entries"></xpather>    </xpather></html>

1 个答案:

答案 0 :(得分:1)

试试这个:

List<WebElement> elementxb1 = driver.findElements(By.xpath("//input[@name='contractPeriod']"));

for (WebElement element : elementxb1){
    boolean vapval2b = element.isSelected();

        if(vapval2b)
        {
                System.out.println("Yes Button is selected ? : " + vapval2b); 
        }
        else 
        {
            System.out.println("No Button is not selected, click on it. ? :" + vapval2b); 
            // click on the radio button here.
            if(element.isDisplayed())
               element.click();
            else{((JavascriptExecutor)driver).executeScript("arguments[0].click();",element);
           }

        }
}

EDIT1:

List<WebElement> elementxb1 = driver.findElements(By.xpath("//input[@name='contractPeriod']"));

for(int i11 = 0 ; i11< elementxb1.size() ; i11++){ 

      elem = elementxb1.get(i11).findElement(By.xpath("//input[@name='contractPeriod']"));
      boolean vapval2b = elem.isSelected();

        if(vapval2b)
        {
                System.out.println("Yes Button is selected ? : " + vapval2b); 
        }
        else 
        {
            System.out.println("No Button is not selected, click on it. ? :" + vapval2b); 
            // click on the radio button here.
        if(elem.isDisplayed())
           elem.click();
        else{((JavascriptExecutor)driver).executeScript("arguments[0].click();",elem);
        }
}