pam027
pam027

Reputation: 11

Python Selenium - Drop down menu

EDIT : SOLVED !

I just started using selenium and I can't seem to figure out how to choose from the drop down menu. I'm trying to choose the Metered Volumes (All) option under the SelectReport but it's giving me an error saying that the SelectReport element does not exist.

Here's the HTML of the site:

<select name="SelectReport" size="1" onchange="populateReportType(document.reportForm, getCurrReportObj())">
              <option selected="" value="NO LINK">
                Select a Report
              </option>
              <option value="NO LINK">
              </option>
            <option value="NO LINK">SETTLEMENT</option>
            <option value="Market/Reports/PublicSummaryAllReportServlet">--- Metered Volumes (All)</option>
            <option value="Market/Reports/DdsPaymentSummaryReportServlet">--- DDS Payment Summary</option>
            <option value="Market/Reports/DdsChargeSummaryReportServlet">--- DDS 
......
            </select>

And here's what I tried so far:

select = Select(self.driver.find_element_by_name("SelectReport"))
select.select_by_value("Market/Reports/PublicSummaryAllReportServlet")

It would be really helpful if someone can help. I've been stuck with this issue for a couple of days now.

Upvotes: 1

Views: 69

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

To select the <option> with text as Metered Volumes (All) using Selenium you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and select_by_visible_text():

    # presuming the actual option text is "Metered Volumes (All)"
    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[name='SelectReport']")))).select_by_visible_text("Metered Volumes (All)")
    
  • Using XPATH and select_by_value():

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='SelectReport']")))).select_by_value("Market/Reports/PublicSummaryAllReportServlet")
    
  • Note : You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions