2

I have the following drop down list that I want to select the value "Previous Year" from:

Html_section

(I've already selected the value manually)

The problem is two fold, I'm having a hard time "finding" or locating this drop down list with my code, and the number in the "id" changes page to page, so I can't just locate it by its "id". I'm trying to use the following Python 3 code:

select = driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]')
select.click()
# then click the "Previous Year" text

and I also tried:

select = Select(driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]'))
select.select_by_value("Previous Year")

Now this site is a private site for work, so I can't grant access, but I'm hoping someone can point out what else I can try to locate this element and select the proper drop down value? I've tried locating this field by "css" and "xpath", no luck.

xpath = //*[@id="dropdown-intervaltype31442"]
css_selector = #dropdown-intervaltype31442
full_xpath = /html/body/div[4]/div[2]/div/div[3]/div[3]/div/div[2]/div/form/table/tbody/tr/td[2]/div/div[1]/select

Any ideas to test would help. Thanks!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
wildcat89
  • 1,159
  • 16
  • 47

2 Answers2

0

The <select> element is a dynamic element. So to select the <option> with text as Previous Year 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:

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[id^='dropdown-intervaltype'][name^='deviceServicesModel']"))))
    select.select_by_visible_text('Previous Year')
    
  • Using XPATH:

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@id, 'dropdown-intervaltype')][starts-with(@name,'deviceServicesModel')]"))))
    select.select_by_visible_text('Previous Year')
    
  • 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
    from selenium.webdriver.support.ui import Select
    

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can use XPath :

new Select (driver.findElement(By.xpath("//select[starts-with(@id, 'dropdown-intervaltype')][starts-with(@name,'deviceServicesModel')]"))).selectByVisibleText("Privious Year");

You can also use id:

new Select(driver.findElement(By.id("dropdownintervaltype"))).selectByVisibleText("Argentina");

You check Your xpath And take correct xpath. I hope this answer is useful for you.