0

I want to test an application in Joomla. I have a dropdown with this code:

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

I'm using Java for testing the site. How can I select from the dropdown the "stanza doppia" option?

ZygD
  • 22,092
  • 39
  • 79
  • 102
Razzo
  • 47
  • 1
  • 6

2 Answers2

1

Have you considered using Select class

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");
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • I try but it dowsn't work :( I have the same problem on a public page: http://bachecalloggi.listedisinistra.org/index.php/annunci this is the code: ` public void testFilterAds() throws Exception { driver.findElement(By.xpath("//button[contains(text(),'Ricerca Avanzata')]")).click(); WebElement elemnet = driver.findElement(By.id("filter_energy_class")); Select select = new Select(elemnet); //By value select.selectByValue("b"); //By index select.selectByIndex(2); //By text select.selectByVisibleText("B"); Thread.sleep(500000); }` – Razzo Jul 11 '15 at 17:47
  • nothing is selected :( – Razzo Jul 11 '15 at 22:26
  • You are not using the same code I am – Saifur Jul 11 '15 at 22:57
  • can you post a completa class to select B in dropdown of filter_energy_class on page http://bachecalloggi.listedisinistra.org/index.php/annunci ? please – Razzo Jul 12 '15 at 10:27
1

I have tried on the above website mentioned by you and it worked for me. Actually you need to use custom xpath to pick the values from the drop-down and store it in a list. Then click the value you want.

Sometime Select() doesn't works, you can use this workaround for selecting the values in drop down.

Here is the working code for the same.

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

        }
}
Hari kishen
  • 463
  • 2
  • 9