2016-04-25 51 views
0
My HTML code is here: 
    <fieldset> 
<div class="clearfix"> 
<div class="clearfix"> 
<div class="clearfix"> 
<div class="clearfix"> 
<div class="qs-formfield-short qs-required"> 
<label for="stateCountry">State or Province</label> 
<span class="k-widget k-dropdown k-header" style="" title="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="stateCountry_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false"> 
<span class="k-dropdown-wrap k-state-default" unselectable="on"> 
<span class="k-input" unselectable="on">Please Select...</span> 
<span class="k-select" unselectable="on"> 
<span class="k-icon k-i-arrow-s" unselectable="on">select</span> 
</span> 
</span> 

我的代碼是在這裏:如何在不使用Select類的情況下從下拉列表中選擇數值,是否因爲列表框中的下拉列表框未選擇?

WebElement stateDropDown = driver.findElement( By.xpath(「/ HTML /體/形式/ DIV [3] /主/ DIV/DIV/DIV /跨度/格/字段集/ DIV [4]/DIV [1] /量程/跨度[1]「)); List options = stateDropDown.findElements(By.xpath(「/ html/body/div [1]/div/ul/li [44]」));

for(WebElement opt : options){ 
     if ("Texas".equals(opt.getText())); 
     opt.click(); 
     System.out.println(opt); 
+0

請張貼所有相關的HTML,從開始一個'

+0

我已經更新了HTML並從

開始。我希望這足以讓你。謝謝 – Asad

+0

請明確說明您的問題。你看到任何結果或你的輸出是錯誤的? –

回答

0

嘗試水木清華這樣的:

Actions action = new Actions(driver);  
WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'k-dropdown-wrap')]")); 
action.moveToElement(optionsList); 

List<WebElement> options = driver.getElemets(By.xpath("//span[contains(@class, 'k-input')]")); 
for(WebElement option : options) { 
    if (option.getText().equals("Texas")) { 
     option.click(); 
    } 
} 

停止寫入奇怪的XPath元素:)

+0

抱歉,我是自動化和學習東西的新手。感謝您的建議和幫助! – Asad