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?

1
  • Creating Select object will be help, which mentioned in the first answer. Commented Jul 11, 2015 at 17:28

2 Answers 2

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

        }
}
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

I try but it dowsn't work :( I have the same problem on a public page: 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); }`
nothing is selected :(
You are not using the same code I am
can you post a completa class to select B in dropdown of filter_energy_class on page bachecalloggi.listedisinistra.org/index.php/annunci ? please

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.