0
   Month 
<select name="filterMonth" onchange="document.form.submit()">
<option value="0"></option>
  <option value="01">January</option>
  <option value="02">February</option>
  <option value="03">March</option>
  <option value="04">April</option>
  <option value="05">May</option>
  <option value="06">June</option>
  <option value="07">July</option>
  <option value="08">August</option>
  <option value="09">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>

How can I select drop down list for "filterMonth" using xpath?

browser.find_element_by_xpath('//*[@id="form"]/table[1]/tbody/tr[3]/td[2]/select[2]') 

gives me error that no such element found

3 Answers 3

0

You can use the xpath

//select[@name='filterMonth'] 

if name is unique across the page.

Select select =new Select(driver.findelement(By.xpath("//select[@name='filterMonth']")));
select.selectByVisibleText("January");

This for Java. please change it accordingly for python.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try following code:

select = Select(driver.find_element_by_xpath("//select[@name='filterMonth']"))

# select by visible text
select.select_by_visible_text('January')

Comments

0

One of the reasons why you get NoSuchElementException might be using tbody tag in your XPath as this tag not always present in initial HTML source, but might be generated dynamically. The other reason is that entire table could be generated dynamically and you should wait a little to be able to handle it:

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

select = wait(browser, 10).until(EC.presence_of_element_located((By.NAME, "filterMonth")))
Select(select).select_by_visible_text("March")

Also you can use provided by @Murthi //select[@name="filterMonth"] XPath to select drop-down

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.