2017-05-03 37 views
0

我有多個單選按鈕,我正在尋找的是獲得Tilte並單擊每個div的單選按鈕。 這是HTML。基於文本訪問電臺裏面的div

<div id="iff-campaigns"> 
    <div id="iff-campaign52249" class="iff-campaign-container iffhidden" onmouseover="IffAdvertismentAction(52249,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52249,&quot;out&quot;);"> 
     <div class="iff-title">Would you like to get paid to take Surveys and Fun IQ Tests?</div> 
     <div class="iff-optin"> 
      <label> 
       <input type="radio" name="iffinput-52249" id="iffinputyes-52249" value="Yes" onclick="IffAdvertismentSelect(52249,false,&quot;&quot;,false);">Yes</label> 
      <label> 
       <input type="radio" name="iffinput-52249" id="iffinputno-52249" value="No" onclick="IffAdvertismentSelect(52249,false,&quot;&quot;,false);">No</label> 
     </div> 
     <div id="div-cq52249" class="iff-div-customquestions-survey"> 
      <div class="clear"></div> 
      <div id="div-first5224915243" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> First Name: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="ifffirst~52249~15243" name="ifffirst~52249~15243" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifffirst~52249~15243&quot;,&quot;first&quot;,1,1);" regfld=""> </div> 
      </div> 
      <div class="clear"></div> 
      <div id="div-last5224915244" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Last Name: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="ifflast~52249~15244" name="ifflast~52249~15244" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifflast~52249~15244&quot;,&quot;last&quot;,1,1);" regfld=""> </div> 
      </div> 
      <div class="clear"></div> 
      <div id="div-email5224915245" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="iffemail~52249~15245" name="iffemail~52249~15245" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52249~15245&quot;,&quot;email&quot;,1,1);" regfld="^[^<>[email protected]]+(@[^<>[email protected]]+(.[^<>[email protected]]+)+)$"> </div> 
      </div> 
      <div class="clear"></div> 
     </div> 
     <div id="iff-rollup52249" class="iff-roleup"> </div> 
    </div> 
    <div id="iff-campaign52706" class="iff-campaign-container" onmouseover="IffAdvertismentAction(52706,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52706,&quot;out&quot;);"> 
     <div class="iff-title">Would you like the days important news broken down into a quick and informative read each morning?</div> 
     <div class="iff-optin"> 
      <label> 
       <input type="radio" name="iffinput-52706" id="iffinputyes-52706" value="Yes" onclick="IffAdvertismentSelect(52706,false,&quot;&quot;,false);">Yes</label> 
      <label> 
       <input type="radio" name="iffinput-52706" id="iffinputno-52706" value="No" onclick="IffAdvertismentSelect(52706,false,&quot;&quot;,false);">No</label> 
     </div> 
     <div id="div-cq52706" class="iff-div-customquestions-survey"> 
      <div class="clear"></div> 
      <div id="div-email5270620435" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="iffemail~52706~20435" name="iffemail~52706~20435" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52706~20435&quot;,&quot;email&quot;,1,1);" regfld="^[^<>[email protected]]+(@[^<>[email protected]]+(.[^<>[email protected]]+)+)$"> </div> 
      </div> 
      <div class="clear"></div> 
     </div> 
     <div id="iff-rollup52706" class="iff-roleup"> </div> 
    </div> 
    <div id="iff-campaign52912" class="iff-campaign-container" onmouseover="IffAdvertismentAction(52912,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52912,&quot;out&quot;);"> 
     <div class="iff-title">Ready to take cooking to the next level? </div> 
     <div class="iff-optin"> 
      <label> 
       <input type="radio" name="iffinput-52912" id="iffinputyes-52912" value="Yes" onclick="IffAdvertismentSelect(52912,false,&quot;&quot;,false);">Yes</label> 
      <label> 
       <input type="radio" name="iffinput-52912" id="iffinputno-52912" value="No" onclick="IffAdvertismentSelect(52912,false,&quot;&quot;,false);">No</label> 
     </div> 
     <div id="div-cq52912" class="iff-div-customquestions-survey"> 
      <div class="clear"></div> 
      <div id="div-first5291222515" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> First Name: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="ifffirst~52912~22515" name="ifffirst~52912~22515" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifffirst~52912~22515&quot;,&quot;first&quot;,1,1);" regfld=""> </div> 
      </div> 
      <div class="clear"></div> 
      <div id="div-last5291222516" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Last Name: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="ifflast~52912~22516" name="ifflast~52912~22516" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifflast~52912~22516&quot;,&quot;last&quot;,1,1);" regfld=""> </div> 
      </div> 
      <div class="clear"></div> 
      <div id="div-email5291222517" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="iffemail~52912~22517" name="iffemail~52912~22517" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52912~22517&quot;,&quot;email&quot;,1,1);" regfld="^[^<>[email protected]]+(@[^<>[email protected]]+(.[^<>[email protected]]+)+)$"> </div> 
      </div> 
      <div class="clear"></div> 
      <div id="div-zip5291222522" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Zip: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="iffzip~52912~22522" name="iffzip~52912~22522" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffzip~52912~22522&quot;,&quot;zip&quot;,1,1);" regfld="(^d{5}$)|(^d{5}-d{4}$)|^([ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ])+{0,1}(d[ABCEGHJKLMNPRSTVWXYZ]d)$"> </div> 
      </div> 
      <div class="clear"></div> 
     </div> 
     <div id="iff-rollup52912" class="iff-roleup"> </div> 
    </div> 
    <div id="iff-campaign52230" class="iff-campaign-container" onmouseover="IffAdvertismentAction(52230,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52230,&quot;out&quot;);"> 
     <div class="iff-title">Would you like to receive FREE Severe Local Weather Alerts straight to your inbox? These can be life saving! </div> 
     <div class="iff-optin"> 
      <label> 
       <input type="radio" name="iffinput-52230" id="iffinputyes-52230" value="Yes" onclick="IffAdvertismentSelect(52230,false,&quot;&quot;,false);">Yes</label> 
      <label> 
       <input type="radio" name="iffinput-52230" id="iffinputno-52230" value="No" onclick="IffAdvertismentSelect(52230,false,&quot;&quot;,false);">No</label> 
     </div> 
     <div id="div-cq52230" class="iff-div-customquestions-survey"> 
      <div class="clear"></div> 
      <div id="div-email5223015020" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="iffemail~52230~15020" name="iffemail~52230~15020" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52230~15020&quot;,&quot;email&quot;,1,1);" regfld="^[^<>[email protected]]+(@[^<>[email protected]]+(.[^<>[email protected]]+)+)$"> </div> 
      </div> 
      <div class="clear"></div> 
      <div id="div-zip5223015025" class="iff-customquestion"> 
       <div class="iff-cqlabel"> <span>*&nbsp;</span> Zip: </div> 
       <div class="iff-cqcontrol"> 
        <input type="text" id="iffzip~52230~15025" name="iffzip~52230~15025" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffzip~52230~15025&quot;,&quot;zip&quot;,1,1);" regfld="(^d{5}$)|(^d{5}-d{4}$)|^([ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ])+{0,1}(d[ABCEGHJKLMNPRSTVWXYZ]d)$"> </div> 
      </div> 
      <div class="clear"></div> 
     </div> 
     <div id="iff-rollup52230" class="iff-roleup"> </div> 
    </div> 
</div> 

我試着以下但不工作:

//div[contains(text(),'" +textvalue+ "')]/preceding:://input[@value='Yes']) 

我需要提取文本,並點擊單選按鈕,每格。

List<WebElement> camp2 = driver.findElements(By.className("iff-title")); 
        System.out.println(camp2.size()); 
        for (WebElement title2: camp2) 
        { 
         System.out.println("Camp Title - "+title2.getText()); 
         String text = title2.getText(); 
         if(text.isEmpty()) 
         { 
          System.out.println("Null Value found"); 
         } 
         else 
         { 
          WebElement div = driver.findElement(By.xpath("//div[contains(text(),'"+text+"')]")); 
          div.findElement(By.xpath("//input[@value='Yes']")).click(); 
          System.out.println("In Else loop."); 
         } 
         //div[contains(text(),'Would you like a Gift Card for a FREE Website, Blog or Online Store?')] 
        } 
+0

燦你只是解釋furthur – Piyush

+0

關於什麼, –

+0

我需要點擊單選按鈕,這是存在於div中,問題是有多個具有相同ID的div,所以我想出的是鎖定基於文本的div,然後移動到該div中的無線電元件,然後單擊它。 –

回答

0

嘗試以下操作:

//for yes radio button 
driver.findElement(By.cssSelector("div.iff-optin label:nth-child(1) input")).click(); 

//for No radio button 
driver.findElement(By.cssSelector("div.iff-optin label:nth-child(2) input")).click(); 

UPDATE:

,如果你只是要檢查是對所有無線電設備,以下是爲您的代碼:

List<WebElement> camp2 = driver.findElements(By.cssSelector("div.iff-optin label:nth-child(1) input")); 

    System.out.println(camp2.size()); 

    for(WebElement ele :camp2){ 
     ele.click(); 


    } 
+0

它只能點擊第一個div的廣播選項: –

+0

列表 camp2 = driver.findElements(By.className(「iff-title」)); \t \t \t \t \t爲(WebElement TITLE2:camp2) \t \t \t \t \t { \t \t \t \t \t \t的System.out.println( 「營標題 - 」 + title2.getText()); (「div.iff-optin label:nth-​​child(1)input」))。click();}。 \t \t \t \t \t} –

+0

那麼結果是什麼? – kushal