0

I am trying to get a value from a dropdown. I have a class that uploads a video which I then want to publish. I need to change the status in order to do that. I saw another person asking a similar question but none of the suggestions seem to work. I am providing the html, call to the method in the class and the method. Any help is greatly appreciated.

Here is what the select looks like on the web page:

<select id="group_media_953_status" class="status" name="group_media_attributes[953][status]" style="display: none;">
    <option value="published" selected="selected">Published</option>
    <option value="scheduled">Scheduled</option>
    <option value="unpublished">Not Published</option>
</select>

Here is my call to a method:

dropdown("group_media_953_status", "Published");

Here is the method:

    public void dropdown(String name, String sel) {
        try {
            WebElement select = driver.findElement(By.id(name));
            List<WebElement> options =    select.findElements(By.tagName("option"));
            for (WebElement option : options) {
                System.out.println(option.getText());
        //     if(sel.equals(option.getText()))
        //         option.click(); 
            }
        } catch (NoSuchElementException ex) {
            System.err.println("Element in dropdown menu was not found");
            driver.quit();
            System.exit(0);
        }
    }

Here is the full html. I did not code the html so I will not be able to change it:

        <td class="group-status">
        <div class="field status">
        <select id="group_media_9870_status" class="status"      name="group_media_attributes[9870][status]" style="display: none;">
    <option value="published" selected="selected">Published</option>
    <option value="scheduled">Scheduled</option>
    <option value="unpublished">Not Published</option>
    </select>

<div id="group_media_9870_status_chosen" class="chosen-container chosen-container-single chosen-container-single-nosearch" style="width: 0px;" title="">
<a class="chosen-single published">
<span>Published</span>
<div>
<b></b>
</div>
</a>
<div class="chosen-drop">
<div class="chosen-search">
<ul class="chosen-results">
</div>

I also tried the xpath and that did not find the select element either. /html/body/div[1]/section/div/div/form[2]/table/tbody/tr[1]/td[3]/div[1]/select and that still did not find the "select" element.

Janet
  • 251
  • 1
  • 2
  • 11
  • suggest you add link to the similar question, just for completeness sake. – James Jones Nov 16 '16 at 00:10
  • http://stackoverflow.com/questions/12940592/how-to-select-an-item-from-a-dropdown-list-using-selenium-webdriver-with-java/14477819#14477819 – Janet Nov 16 '16 at 12:38

1 Answers1

0

Have you tried using the Select class?

import org.openqa.selenium.support.ui.Select;

public void dropdown(String name, String sel) {
        try {
              Select select = driver.findElement(By.id(name));
              select.selectByValue(sel);
        } catch (NoSuchElementException ex) {
            System.err.println("Element in dropdown menu was not found");
            driver.quit();
            System.exit(0);
        }
    }
  • Yes. That was one of the options in the post that I had found. What is happening is that it is not finding the "select" element on the page. I have tried using the class name, xpath, id, and name and none seem to work. I have spent all day on this and this really should not be that difficult. There are other similar items where the dropdown action was used rather than the select but the developer chose select for some reason here. :( – Janet Nov 15 '16 at 20:09
  • Take a look at the first snippet of html you posted and the second. Your select object appears to have a dynamic number in it group_media_'953'_status. Try writing an xpath like .//div[@class='field status']/select and using that instead of the id for the select – Shawn Gross Nov 16 '16 at 19:07