user3788728 user3788728 - 1 year ago 104
Python Question

python selenium select from drop-down menu

I am trying to select the "CAS Number" option on this website. From reading other posts, I have written several Python code using Selenium

driver.find_element_by_css_selector("select#selectbox > option[value='cas']").click()

driver.find_element_by_xpath("//select[@id ='selectbox']/option[@value ='cas']").click()

box = driver.find_element_by_id('selectbox')
for option in box.find_elements_by_tag_name('option'):
if option.text == 'cas':

But they all fail to select the appropriate box. So I am wondering where the problem is.

Answer Source

First click on the input, then click the list item you need. Example, for CAS Number:

from selenium import webdriver

driver = webdriver.Firefox()

element = driver.find_element_by_id('selectbox_input')

li = driver.find_element_by_id('selectbox_input_cas')

Note that it is not a regular select tag that could be much easier operated with using