Speedychuck
Speedychuck

Reputation: 400

Select Drop Down - Selenium Webdriver

I am creating practice tests on a demo site, however, I am having an issue with selecting a value from a drop down list, I am getting unable to locate element, however, it is the correct ID and I've tried by ID and CSS selector as well with no luck :( I will post HTML and Selenium code below:

HTML

<select id="dropdown_7" name="dropdown_7" class="  piereg_validate[required]"><option value="Afghanistan">Afghanistan</option>

Ruby Code:

drop_list = @@wait.until {
          drop = @@driver.find_element :id => '#dropdown_7'
          drop if drop.displayed?
          drop.click
        }


        options=drop_list.find_element :id => '#dropdown_7'

        options.each do |i|
          if i.text == 'American Samoa'
            i.click
            break
          end

Upvotes: 0

Views: 1286

Answers (4)

akhilesh gulati
akhilesh gulati

Reputation: 128

Use JavaScript executor for clicking the options in the list.

public void javascriptclick(String element)
{ 
    WebElement webElement=driver.findElement(By.xpath(element));
    JavascriptExecutor js = (JavascriptExecutor) driver;

    js.executeScript("arguments[0].click();",webElement);   
    System.out.println("javascriptclick"+" "+ element);
}

Upvotes: 0

mbround18
mbround18

Reputation: 1

How I've implemented this in ruby is:

Selenium::WebDriver::Support::Select.new(
    @driver.find_element(:how, :what)
).select_by(:how, :what)

Upvotes: 0

Justin Ko
Justin Ko

Reputation: 46836

The problem is that you are specifying the id as "#dropdown_7". While this is a CSS-selector that matches an element with id "dropdown_7", it will not match the id attribute.

It should just be:

drop = @@driver.find_element :id => 'dropdown_7'

Upvotes: 1

murali selenium
murali selenium

Reputation: 3927

In java below is the way to select dropdowns

  Select dropdown = new Select(driver.findElement(By.id("your id")));

  dropdown.selectByVisibleText("option text here");

or

   dropdown.selectByIndex(1);

or

   dropdown.selectByValue("value attribute of option");

So no need to click on option by using Select in webdriver.

Thank You, Murali

Upvotes: 0

Related Questions