2016-05-20 13 views
0

Ich möchte eine Seite mit Kontrollkästchen ohne ID zu kratzen, haben sie den gleichen Namen und nur die Werte sind differents.Wie mit Checkbox Selen Webdriver umgehen?

<div class="mvNavLk"> 
    <form class="jsExpSCCategories" method="post" name="ExpressSCCategories" action="actionTest.html"> 
     <ul class="mvSrcLk"> 
      <li> 
       <label class="mvNavSel mvNavLvl1"> 
        First 
        <input type="checkbox" value="firstValue" name="selectedNavigationCategoryPath"> 
       </label> 
      </li> 
      <li> 
       <label class="mvNavSel mvNavLvl1"> 
        Second 
        <input type="checkbox" value="secondValue" name="selectedNavigationCategoryPath"> 
       </label> 
      </li> 
     </ul> 
    </form> 
</div> 
+0

Was möchten Sie einstellen? Auf welches Element willst du zugreifen? – theRoot

+0

@theRoot Ich möchte den Wert für zum Beispiel "firstValue" in das Kontrollkästchen, – parik

+0

setzen Sie den Wert oder klicken Sie auf das Kontrollkästchen mit Wert firstValue –

Antwort

1

Verwendung unter Code:

driver.find_element_by_css_selector(".mvSrcLk>li:nth-child(1)>label.mvNavSel.mvNavLvl1").click(); 

Hoffnung dies funktionieren wird.

+0

Ich aktualisierte meine Frage mit meinem Code, es funktioniert nicht – parik

+1

Ich aktualisierte meine Antwort, bitte überprüfen Sie die aktualisierte Antwort . – noor

+0

Vielen Dank, es funktioniert, also müssen wir nicht bewerten? – parik

0
driver.findElement(By.name("selectedNavigationCategoryPath")).click(); 
+0

es funktioniert nicht, weil sie den gleichen Namen haben – parik

1

Hallo bitte tun Sie es wie unten Hinweis dieses Beispiel in Java ist

// take check boxes with same name inside the list 
List<WebElement> myCheckBox = driver.findElements(By.name("selectedNavigationCategoryPath")); 

// now on the basis of index call click 

myCheckBox.get(0).click(); // for the first check box 
Thread.sleep(2000); 
myCheckBox.get(1).click(); // for the second check box 

oder wenn Sie wollen dann auf der Basis von Wert wählen

driver.findElement(By.xpath("//*[@value='firstValue']")).click(); // for the 1st one 
Thread.sleep(2000); 
driver.findElement(By.xpath("//*[@value='secondValue']")).click(); // for the 2st one 

UPDATE

WebDriverWait wait = new WebDriverWait(driver,30); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@value='firstValue']"))); 
driver.findElement(By.xpath("//*[@value='firstValue']")).click(); // for the 1st one 

Hoffnung, dies hilft Ihnen

+0

Ich habe nicht die Anzahl der Checkboxen, – parik

+0

das ist, warum ich es in der Liste zuerst genommen habe, nimmt alles in Liste Java gibt jedem Wert einen Indexwert und in Java Index beginnt Form 0 so für 1. Kontrollkästchen Index ist 0, für zweite Kontrollkästchen Index ist 1 und so weiter –

+0

Ich habe diesen Fehler: ElementNotVisibleException: Nachricht: Element ist derzeit nicht sichtbar und so möglicherweise nicht mit – parik

0
Below Code is in C#, Collects all the Checkboxes with the name specified. Then Iterates through each ckeckbox, gets the value attribute, if the attribute is equal to your specified check box, then clicks it and comes out of the loop. Hope this should work. 
IList<IWebElement> myCheckBoxs = driver.FindElements(By.name("selectedNavigationCategoryPath")); 

Foreach(IWebElement chkBx in myCheckBoxs) 
{ 
    if(chkBx.GetAttribute("Value")=="Your Desired value") 
    { 
    chkBx.Click(); 
    break; 
    } 
} 

`

1

Hope this Werke-

driver.FindElement(By.XPath(".//label[contains(text(),'First')]/input")).SendKeys("test"); 
0

verwenden CSS-Locator.

[name='selectedNavigationCategoryPath'][value='firstValue']