Note that there are some explanatory texts on larger screens.

plurals
  1. POSelenium: Matching xpath from strings in an array
    text
    copied!<p>With Selenium WebDriver I'm attempting to select drop down menu elements on a webpage by strings read from an Excel file: </p> <pre><code> Xls_Reader data = new Xls_Reader("src//com//testexcel//data//data3.xlsx"); String values = data.getCellData("DropList", "Index_Value", 2); String selections[] = values.split(","); </code></pre> <p>They are in this form: Construction,Engineering,Legal,etc.</p> <p>Each element I am trying to select looks like this:</p> <pre><code>&lt;div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;"&gt; &lt;input id="ddcl-selInd-i3" class="active" type="checkbox" tabindex="0" index="3" value="11"&gt; &lt;label class="ui-dropdownchecklist-text" for="ddcl-selInd-i3" style="cursor: default;"&gt;Construction&lt;/label&gt; &lt;/div&gt; &lt;div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;"&gt; &lt;input id="ddcl-selInd-i5" class="active" type="checkbox" tabindex="0" index="5" value="03"&gt; &lt;label class="ui-dropdownchecklist-text" for="ddcl-selInd-i5" style="cursor: default;"&gt;Engineering&lt;/label&gt; &lt;/div&gt; </code></pre> <p>Here is the code:</p> <pre><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&lt;WebElement&gt; iList = driver.findElements(By.xpath("//*[@id='ddcl-selInd-ddw']")); for (int i=0; i&lt;selections.length; i++) { driver.findElement(By.xpath("//*[text()='" + selections[i] + "']")).click(); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload