Exercise 4 from module 13 help | Selenium Forum
M
Posted on 14/04/2016
Hi

I attached the code snapshot. I could find out the check boxes but along with the right label text, I don't know know how to get 2 siblings checks in one xpath expression. see attachments

M
Replied on 15/04/2016

.equalsignorecase


M
Replied on 17/04/2016

It doesn't help sir. The xpath bring all the checkbox to click, not only the Engineering text

WebElement dropdownlist = driver.findElement(By.xpath("//*[@id='ddcl-selInd']/span"));
dropdownlist.click();

String desiredOption = "Engineering";// Manufacturing
String xpathexpr = "//*[@id='ddcl-selInd-ddw']/div/*";
//uncheck the select all industries check box first
System.out.println("uncheck ============================");
selectCheckbox("Select All Industries",xpathexpr);
System.out.println("select engineering============================");
//select Engineering
selectCheckbox(desiredOption,xpathexpr);


}
public static void selectCheckbox(String optionname,String xpathexpr){
List<WebElement> alloptions = driver.findElements(By.xpath(xpathexpr));
//System.out.println("total ==="+alloptions.size());

for (int i=0;i<alloptions.size();i++){
String text = alloptions.get(i).getText();
System.out.println("checked options name ====="+text);
if (optionname.equalsIgnoreCase(text)){
WebElement checkbox = alloptions.get(i).findElement(By.xpath("//input[@type='checkbox']"));
checkbox.click();
System.out.println("checked checkbox ====="+checkbox.getAttribute("checked"));

}//if

}//for
}

Please let me know the right xpath that point to only the checkbox assoicated with specified text provided like Engineering
Thanks


M
Replied on 17/04/2016

send me the entire code.


M
Replied on 19/04/2016

attached code


M
Replied on 20/04/2016

use this

[quote:8odt4wsy]package com.sample;

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 exercise4 {
static WebDriver driver = null;

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.jobserve.com/us/en/Job-Search/");

WebElement dropdownlist = driver.findElement(By
.xpath("//*[@id='ddcl-selInd']/span"));
dropdownlist.click();

String desiredOption = "Engineering";// Manufacturing
String xpathexpr = "//*[@id='ddcl-selInd-ddw']/div/*";
// uncheck the select all industries check box first
System.out.println("uncheck ============================");
selectCheckbox("Select All Industries", xpathexpr);
System.out.println("select engineering============================");
// select Engineering
selectCheckbox(desiredOption, xpathexpr);
driver.quit();
}

public static void selectCheckbox(String optionname, String xpathexpr) {
List<WebElement> alloptions = driver.findElements(By.xpath(xpathexpr));
// System.out.println("total ==="+alloptions.size());

for (int i = 0; i < alloptions.size(); i++) {
String text = alloptions.get(i).getText();
System.out.println("checked options name =====" + text);
if (optionname.equalsIgnoreCase(text)) {

[color=#FF0000:8odt4wsy]alloptions.get(i).click();
[/color:8odt4wsy]
System.out.println("checked checkbox ====="
+ checkbox.getAttribute("checked"));

}// if

}// for
}
}[/quote:8odt4wsy]


M
Replied on 23/04/2016

Thank you it works