0

I've written a script to select certain field from a webpage using python with selenium. There is a dropdown on that page from which I want to select "All". However, i tried many different ways with my script to make it but could not. Here is how the dropdown look like.

enter image description here

Html elements for the dropdown selection:

<select name="ctl00$body$MedicineSummaryControl$cmbPageSelection" onchange="javascript:setTimeout('__doPostBack(\'ctl00$body$MedicineSummaryControl$cmbPageSelection\',\'\')', 0)" id="ctl00_body_MedicineSummaryControl_cmbPageSelection">
        <option selected="selected" value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="all">All</option>

    </select>

Scripts I've tried with:

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('http://apps.tga.gov.au/Prod/devices/daen-entry.aspx')

driver.find_element_by_id('disclaimer-accept').click()
time.sleep(5)
driver.find_element_by_id('medicine-name').send_keys('pump')
time.sleep(8)
driver.find_element_by_id('medicines-header-text').click()
driver.find_element_by_id('submit-button').click()
time.sleep(7)

 #selection for the dropdown should start from here

driver.find_element_by_xpath('//select[@id="ctl00_body_MedicineSummaryControl_cmbPageSelection"]').click()
driver.find_element_by_xpath('//select//option[@value]').send_keys("All")

3 Answers 3

2

This will work for you:

#option1
select_obj = Select(driver.find_element_by_xpath('//select[@id="ctl00_body_MedicineSummaryControl_cmbPageSelection"]'))
select_obj.select_by_visible_text('All')
#option2
select_obj = Select(driver.find_element_by_id('ctl00_body_MedicineSummaryControl_cmbPageSelection'))
select_obj.select_by_visible_text('All')

And don't forget to import Select with from selenium.webdriver.support.ui import Select

You can read full documentation to find all Select methods here : https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.select.html

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

4 Comments

Thanks Alex Lucaci, for your answer. Unfortunately it throws the following error: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
Yes, I am sorry, I've updated my code, it should work now.
Follow this link to see the full error: "dropbox.com/s/qkeqjtcj0w2g06s/Error%20I%27m%20having.txt?dl=0"
Oh yes, I didn't noticed that you are using the find_element_by_id and as a parameter you are passing the xpath so you have 2 choices: select_obj = Select(driver.find_element_by_id('ctl00_body_MedicineSummaryControl_cmbPageSelection')) or select_obj = Select(driver.find_element_by_xpath('//select[@id="ctl00_body_MedicineSummaryControl_cmbPageSelection"]')) . Let me know if you it's working.
0

I initially thought of suggesting that you try to tab from an element that is before the dropdown select, similar to the concept in this code:

driver.find_element_by_id('<id of element before the dropdown select>').send_keys(Keys.TAB)
driver.find_element_by_id('//select[@id="ctl00_body_MedicineSummaryControl_cmbPageSelection"]').send_keys('AL')
driver.find_element_by_id('//select[@id="ctl00_body_MedicineSummaryControl_cmbPageSelection"]').send_keys(Keys.ENTER + Keys.TAB)

However, errors from the stack trace may show you that the dropdown select is not being found with that id. I believe that you should verify that the id that you are using is the correct id for that element, when you record the action of clicking on the dropdown select and selecting an option, by using the Record option in the Selenium IDE.

Comments

0

Found the workaround finally. Here is what I did:

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('http://apps.tga.gov.au/Prod/devices/daen-entry.aspx')

driver.find_element_by_id('disclaimer-accept').click()
time.sleep(5)
driver.find_element_by_id('medicine-name').send_keys('pump')
time.sleep(8)
driver.find_element_by_id('medicines-header-text').click()
driver.find_element_by_id('submit-button').click()
time.sleep(7)

driver.find_element_by_xpath('//select[@id="ctl00_body_MedicineSummaryControl_cmbPageSelection"]').click()
driver.find_element_by_xpath('.//option[@value="all"]').click()
time.sleep(10)

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.