如何使用 selenium webdriver 处理复选框?
How to deal with checkbox using selenium webdriver?
我想抓取一个没有 id 复选框的页面,它们具有相同的名称,只是值不同。
<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>
driver.findElement(By.name("selectedNavigationCategoryPath")).click();
您好,请按照下面的方式操作注意这个例子在java
// 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
或者,如果您想 select 基于值,则
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
更新
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
希望对您有所帮助
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;
}
}
`
希望这有效-
driver.FindElement(By.XPath(".//label[contains(text(),'First')]/input")).SendKeys("test");
使用这个 CSS 定位器。
[name='selectedNavigationCategoryPath'][value='firstValue']
使用下面的代码:
driver.find_element_by_css_selector(".mvSrcLk>li:nth-child(1)>label.mvNavSel.mvNavLvl1").click();
希望这会奏效。
我想抓取一个没有 id 复选框的页面,它们具有相同的名称,只是值不同。
<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>
driver.findElement(By.name("selectedNavigationCategoryPath")).click();
您好,请按照下面的方式操作注意这个例子在java
// 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
或者,如果您想 select 基于值,则
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
更新
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
希望对您有所帮助
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;
}
}
`
希望这有效-
driver.FindElement(By.XPath(".//label[contains(text(),'First')]/input")).SendKeys("test");
使用这个 CSS 定位器。
[name='selectedNavigationCategoryPath'][value='firstValue']
使用下面的代码:
driver.find_element_by_css_selector(".mvSrcLk>li:nth-child(1)>label.mvNavSel.mvNavLvl1").click();
希望这会奏效。