Select checkbox with label | Selenium Forum
M
Posted on 11/02/2017
I want answer for below exercise problem in module 13:
[b:2k1twyub]Go to jobserve.com
Click on industry droplist
Uncheck all by clicking on- "Select All Industries"
"Write a code which will click on checkboxes in front of:
Engineering
Manufacturing"
"Code should be dynamic enough. If we want to click on different
selection tomorrow, we should be able to do it without changing code. We should just need to supply the text against which we want to select "

String company="Manufacturing"[/b:2k1twyub]

I am able to select values in dropdown with checbox directly but not with values infront of it. Please provide solution.

M
Replied on 11/02/2017

package com.soapuitutorial.propertie;

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.firefox.FirefoxDriver;

public class Jobserve {

public static void main(String[] args) {
WebDriver d = new FirefoxDriver();
d.get("http://www.jobserve.com");
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
d.findElement(By.xpath("//span[@id='ddcl-selInd']")).click();
d.findElement(By.xpath("//label[@class='ui-dropdownchecklist-text']"))
.click();
List<WebElement> options = d.findElements(By
.xpath("//div[@id='ddcl-selInd-ddw']/div/div"));
System.out.println(options.size());
String Company = "Education";
for (int i = 1; i < options.size(); i++) {
// System.out.println("Option names: "+options.get(i).getText());
if (Company.equals(options.get(i).getText())) {
options.get(i).click();
}
}
List<WebElement> options1 = d.findElements(By
.xpath("//div[@id='ddcl-selInd-ddw']/div/div/input"));
System.out.println(options1.size());
for (int i = 1; i < options1.size(); i++) {
System.out.println(options.get(i).getText() + "---"
+ options1.get(i).getAttribute("checked"));
}
d.quit();
}
}