2017-10-18 174 views
-4

我如何能夠使用硒webdriver獲得多個複選框標籤名稱?如何使用硒webdriver獲得多個chekcbox標籤名稱?

這裏是網站url

+3

的可能的複製[如何在Java中選擇多個複選框與webdriver的和驗證?](https://開頭計算器.com/questions/27954087/how-to-select-multiple-check-box-with-webdriver-in-java-and-verify) –

+1

歡迎來到Stack Overflow!請參閱:[我如何做X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-dox )對SO的期望是,用戶提出的問題不僅僅是研究來回答他們自己的問題,而且還分享研究,代碼嘗試和結果。這表明你已經花時間去嘗試幫助自己,它使我們避免重申明顯的答案,最重要的是它可以幫助你得到更具體和相關的答案!另見:[問] – JeffC

回答

0

假設我想要得到的所有複選框標籤名稱組名爲Multiple Checkbox Group Test 1

driver.get("http://www.javascriptsource.com/forms/check-uncheck-multiple-checkboxes.html"); 
List<WebElement> all = driver.findElements(By.xpath("//legend[text()='Multiple Checkbox Group Test 1']/..//following-sibling::label")); //get the all labels from particular group 
System.out.println(all.size()); 

for(int i=0; i<all.size(); i++) 
{ 
    System.out.println(all.get(i).getText()); 
    Thread.sleep(2500); 
} 
+0

你有沒有試過這段代碼? –

+0

謝謝你我已經得到答案,並瞭解 –

+0

如果問題得到解決,請將此答案標記爲「已接受」。 :) –

0
With reference to shared link,<br/> 

code: <br/> 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class test { 

    public static void main(String[] args) { 
     System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); //input[@type='checkbox']/following-sibling::label 
     WebDriver driver = new ChromeDriver(); 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
     driver.get("http://www.javascriptsource.com/forms/check-uncheck-multiple-checkboxes.html"); 
     List<WebElement> checkboxLabels = driver.findElements(By.xpath("//input[@type='checkbox']/following-sibling::label")); 
     for(WebElement checkboxLabel: checkboxLabels) { 
      System.out.println(checkboxLabel.getText()); 
     } 
    } 
} 

output: <br/> 
chkboxarray 1<br/> 
chkboxarray 2<br/> 
chkboxarray 3<br/> 
extrachkbox 1<br/> 
groupa 1<br/> 
groupa 2<br/> 
groupa 3<br/> 
groupb 1<br/> 
groupb 2<br/> 
groupb 3