2015-07-11 197 views
0

我想在Joomla中測試應用程序。 我有這個代碼下拉:從下拉列表中選擇元素

<div class="control-group"> 
    <div class="control-label"> 
     <label id="jform_category-lbl" for="jform_category" class="required"> 
      Categoria<span class="star">&#160;*</span> 
     </label> 
    </div> 
    <div class="controls"> 
     <select id="jform_category" name="jform[category]" class="required" required aria-required="true"> 
      <option value="9">stanza singola</option> 
      <option value="10">stanza doppia</option> 
      <option value="11">posto letto</option> 
     </select> 
    </div> 
</div> 

我使用Java進行測試的站點。 如何從下拉列表中選擇「節數百分比」選項?

+0

創建'選擇'對象將是幫助,在第一個答案中提到。 – Turcia

回答

1

我曾嘗試你所提到的上述網站上,它的工作爲我考慮。 實際上,您需要使用自定義xpath從下拉列表中選擇值並將其存儲在列表中。然後點擊你想要的值。

Sometime Select()不起作用,您可以使用此解決方法在下拉列表中選擇值。

以下是相同的工作代碼。

import java.util.List; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class SelectDropdown { 

    public static void main(String[] args) { 

     WebDriver driver = new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
     driver.manage().window().maximize(); 
     driver.get("http://bachecalloggi.listedisinistra.org/index.php/annunci"); 
     driver.findElement(By.xpath("//button[contains(text(),'Ricerca Avanzata')]")).click(); 
     select1(driver); 
    } 

     public static void select1(WebDriver driver) { 
        //Clicking on the Element to open the dropdown. 
        WebElement clickme = driver.findElement(By.xpath("//*[@id='filter_energy_class_chzn']/a/span")); 
        clickme.click(); 
        //Sometime need to wait, as it take some time to load the values in dropdown. 
        //Thread.sleep(3000); 

        //Picking all the value from Dropdown. Use Custom Xpath for this. 
        List<WebElement> options = driver.findElements(By.xpath("//*[@id='filter_energy_class_chzn']//*[@class='chzn-results']/li")); 

        for (int i=0; i<options.size(); i++){ 
        if (options.get(i).getText().equalsIgnoreCase("B")){ 
        options.get(i).click(); 
        } 
        }   

     } 
} 
1

你有沒有使用Select

WebElement elemnet = driver.findElement(By.id("jform_category")); 
Select select = new Select(elemnet); 
//By value 
select.selectByValue("10"); 

//By index 
select.selectByIndex(2); 

//By text 
select.selectByVisibleText("stanza doppia"); 
+0

我嘗試,但它dowsn't工作:( 我有一個公共頁面上的同一個問題:http://bachecalloggi.listedisinistra.org/index.php/annunci 這是代碼: ' 公共無效testFilterAds ()拋出異常{ \t driver.findElement(By.xpath( 「//按鈕[含有(文本(), 'Ricerca Avanzata')]」))點擊(); \t \t WebElement elemnet = driver.findElement (By.id( 「filter_energy_class」)); 選擇選擇=新選擇(elemnet); // 通過值 select.selectByValue( 「b」); //通過索引 select.selectByIndex(2); //通過文本 select.selectByVisibleText(「B」); \t Thread.sleep(500000); }' – Razzo

+0

什麼是錯誤? – Saifur

+0

沒有選擇任何東西:( – Razzo