1

With Selenium WebDriver I'm attempting to select drop down menu elements on a webpage by strings read from an Excel file:

        Xls_Reader data = new Xls_Reader("src//com//testexcel//data//data3.xlsx");
        String values = data.getCellData("DropList", "Index_Value", 2);
        String selections[] = values.split(",");

They are in this form: Construction,Engineering,Legal,etc.

Each element I am trying to select looks like this:

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input id="ddcl-selInd-i3" class="active" type="checkbox" tabindex="0" index="3" value="11">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i3" style="cursor: default;">Construction</label>
</div>

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input id="ddcl-selInd-i5" class="active" type="checkbox" tabindex="0" index="5" value="03">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i5" style="cursor: default;">Engineering</label>
</div>

Here is the code:

package com.selftechy.parameterization;

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JobServe {

    static WebDriver driver = new FirefoxDriver();

    public static void main(String[] args) throws InterruptedException {


        driver.get("https://www.jobserve.com/gb/en/Candidate/Home.aspx");
        driver.findElement(By.xpath(".//*[@id='ddcl-selInd']/span")).click();

        readExcelWords();


    public static void readExcelWords() {
        Xls_Reader data = new Xls_Reader("src//com//testexcel//data//data3.xlsx");
        String values = data.getCellData("DropList", "Index_Value", 2);
        String selections[] = values.split(",");

//help  
List<WebElement> iList = driver.findElements(By.xpath("//*[@id='ddcl-selInd-ddw']"));
    for (int i=0; i<selections.length; i++) {
    driver.findElement(By.xpath("//*[text()='" + selections[i] + "']")).click();

I know the xpath is wrong and possibly the way I am working with data types. I need a way of making xpath selection work on the basis of the array values. I am relatively new to Java and Selenium and would appreciate some help.

2 Answers 2

2

Try

findElement(By.xpath("//label[.='xyz']/../input"));

where xyz is one of Construction, Engineering, etc

Sign up to request clarification or add additional context in comments.

Comments

0

I will suggest try using names and use

findElement(By.name("xyz"));

after using name attribute in your input code.

Something like -

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input name ="Construction" id="ddcl-selInd-i3" class="active" type="checkbox" tabindex="0" index="3"value="11">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i3" style="cursor: default;">Construction</label>
</div>

I am able to find an element with this method using some pseudo code like the one given above.

5 Comments

It didn't work but thanks. I tried: driver.findElement(By.name("Engineering")).click(); which gave org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"Engineering"} Any ideas?
Hi! I edited the code adding the name attribute to the input tag. This is working for me with similar codes. Let us know if it works for you. :)
This works, thanks a lot! I'd like to learn more about how to formulate xpaths like this. Firebug helps but it couldn't give this format- '/../input' for example. Any suggestions would be welcome...
Great to know it works. Well even I am not good at formatting xpath. I use Xpath-Finder an add-on for Firebug (find it here) to find my xpaths. These xpaths sometime needs a bit editing to match with the xpath formats generated when I export my IDE test cases in JUnit4. I would suggest you to ask a separate question for others to answer effectively and accept any answer here for other users who are seeking a solution. :)
If it works you may like to accept the answers for other to know it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.