Reputation: 123
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.
Upvotes: 1
Views: 4294
Reputation: 3404
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.
Upvotes: 0
Reputation: 455
Try
findElement(By.xpath("//label[.='xyz']/../input"));
where xyz
is one of Construction, Engineering, etc
Upvotes: 2