3

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':
    option.select()

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

user3788728
  • 59
  • 1
  • 3
  • 9

3 Answers3

3

After inspecting the DOM of that website, <select id="selectbox" name="focus" style="display: none;"> isn't the one people see in the UI.

The actual drop-down menu is:

<div id="selectbox_container" class="selectbox-wrapper" style="display: none; width: 150px;">
   <ul>
      <li id="selectbox_input_product" class="selected">Product Name or Number</li>
      <li id="selectbox_input_cas">CAS Number</li>
      <li id="selectbox_input_mdl">MDL Number</li>
      <li id="selectbox_input_msds">MSDS</li>
      <li id="selectbox_input_cofa">Certificate of Analysis</li>
      <li id="selectbox_input_formula">Molecular Formula</li>
      <li id="selectbox_input_keyword">Keyword</li>
   </ul>
</div>

Hence please give the following code a try:

driver = webdriver.Chrome()
driver.get("http://www.strem.com/")

driver.find_element_by_id("selectbox_input").click()
driver.find_element_by_id("selectbox_input_cas").click()
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • thank you very much for your kind clarification. this code works great! do you mind explaining how I can tell what people actually see in the UI? – user3788728 Jun 30 '14 at 21:46
  • ummmmm... both you and the previous user gave me similar codes that work, so i hope you wouldn't mind me giving the "check" to alecxe >_<. I really appreciate all the help that you are giving me on my python journey!!!! – user3788728 Jun 30 '14 at 21:48
  • 1
    @user3788728: That's ok. alecxe answered one minute before me. ;(. You can still upvote me by clicking up arrow. Thanks. You can use Firebug or Chrome Developer Tool to inspect DOM and figure the structure. – Yi Zeng Jun 30 '14 at 21:50
2

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

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.strem.com')

element = driver.find_element_by_id('selectbox_input')
element.click()

li = driver.find_element_by_id('selectbox_input_cas')
li.click()

Note that it is not a regular select tag that could be much easier operated with using selenium.webdriver.support.select.Select.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • i see. so i need to first click open the dropdown menu before selecting the item of interest. Thank you so much for your help. The code works! – user3788728 Jun 30 '14 at 21:45
  • when you said that "it is not a regular select tag," are you referring to the fact that we need to first click open the select box before selecting for the item of interest? I do not know when I can use it and when I can't. Thank you. – user3788728 Jun 30 '14 at 21:53
  • @user3788728 yeah, it is basically a `div` with a list inside. The actual `select` is hidden and used under the hood. There is nothing wrong in using the solution in the answer. It is basically the same as you do manually in the browser. – alecxe Jun 30 '14 at 21:56
  • Oh I see. It made more sense now. That's what the li stands for. My friend and I had a hard time yesterday and were researching how to use xpath on the li section. This way is easier to use and understand. Thanks again!! – user3788728 Jun 30 '14 at 22:32
0
theMenu = self.browser.find_element_by_link_text('Title of Menu')
theMenu.click()
menuItem = self.browser.find_element_by_link_text('Title of Menu Item')
menuItem.click()
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Deepak M
  • 11
  • 1