简体   繁体   中英

Accessing drop down list- selenium webdriver & java

I am trying to write a few test cases for automation testing as a part of practice assignment in selenium webdriver using java.

The java version is 1.6 and selenium webdriver version is 2.39 while firefox browser version is 29.0.1.

I am trying to access the drop down titled CARSIZE in the following link:

http://www.carrentals.com/

I am not able to manipulate it.

I have tried the following code...

    driver.get("http:\\www.carrentals.com/");

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    Select dropdown= new Select(driver.findElement(By.xpath("//*[@name='carType']")));

    dropdown.selectByValue("carsize-1");

With the above code, it seems that I am able to find the element(as no exception is thrown) but not change a value. When I try to change a value by SELECTBYVALUE method, I get an exception saying element not visible. Can someone help me?

The Html code for the above can be seen in firebug and just for information I have also tried using ID and name instead of XPath for the concerned select box but I get the same exception.

Try this:

Select dropdown= new Select(driver.findElement(By.xpath("//select[@id='cartype']")));
dropdown.selectByVisibleText("Small Cars");

Updated.

Try this code. It works for me.

    String dropdownXpath = "//label[@for='cartype']/following-sibling::div[@role='listbox']";
    WebElement textInDropDown = webDriver.findElement(By.xpath(dropdownXpath + "//div[@class='text']"));
    textInDropDown.click();
    webDriver.findElement(By.xpath(dropdownXpath)).sendKeys("Small Cars");

It locates text in dropdown element and then sends value which you want to select.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM