2012-08-11 60 views
1

硒的webdriver我試圖通過選擇從Excel文件中讀取字符串下拉網頁上的菜單元素:硒:從匹配的XPath字符串數組中的

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

他們以這種形式:建築,工程,法律等。

每個元素我想選擇看起來是這樣的:

<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> 

下面是代碼:

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(); 

我知道XPath是錯誤的,可能的方式,我處理數據類型。我需要一種基於數組值來進行xpath選擇的方法。我對Java和Selenium相對來說比較陌生,希望得到一些幫助。

回答

1

嘗試

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

其中xyz是建築,工程的一個,等

0

我會建議嘗試使用名稱和使用

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

在你輸入的代碼使用屬性之後。

喜歡的東西 -

<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> 

我能夠找到使用像上面給出的一些僞代碼,此方法的元素。

+0

它沒有工作,但感謝。我試過:driver.findElement(By.name(「Engineering」))。click();這給了org.openqa.selenium.NoSuchElementException:無法找到元素:{「method」:「name」,「selector」:「Engineering」}任何想法? – 3lysium 2012-08-11 23:45:13

+0

您是否在標籤代碼中使用了名稱? – 2012-08-12 07:38:45

+0

嗨!我編輯了將name屬性添加到輸入標籤的代碼。這對我來說有類似的代碼。讓我們知道它是否適合您。 :) – 2012-08-12 07:48:24