0

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

4 Answers 4

1

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

How I've implemented this in ruby is:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.