0

I have the drop-down menu for which the source is as below:

<select name="issuer">
<option selected="selected" value="15">MBBTampereRootCA
</option><option value="66222">OMS_CA1
</option><option value="66225">OMS_CA2
</option><option value="71463">stefanSpiel
</option></select>

and I need to select "stefanSpiel" please tell me how can i do that ??

I tried with Multiple possible options, but not successful.

I have tried these options:

browser = webdriver.Firefox()
browser.find_element_by_css_selector("option.stefanSpiel")
browser.find_element_by_link_text('option.stefanSpiel');

and also these:

'element = browser.find_element_by_name("issuer")'
'target = select (option, stefanSpiel)'
'action_chains = ActionChains(browser)'
'action_chains.drag_and_drop(element, target)'

'ActionChains(browser).move_to_element(element).click(target).perform()'

But all i got is : 'selenium.common.exceptions.NoSuchElementException:'

Thanks,

4
  • Show us what you have tried. Commented Jul 7, 2014 at 13:49
  • I don't know about python but I have done that in Java, could it help you if I paste it here if you know how to adapt it to python ? Commented Jul 7, 2014 at 14:20
  • With a bit more html it might be easier to help you. The containing select element may have an id that could make it quite simple to get it via selenium. Commented Jul 7, 2014 at 20:32
  • I have tried these options: browser = webdriver.Firefox() browser.find_element_by_css_selector("option.stefanSpiel") browser.find_element_by_link_text('option.stefanSpiel'); and also these: 'element = browser.find_element_by_name("issuer")' 'target = select (option, stefanSpiel)' 'action_chains = ActionChains(browser)' 'action_chains.drag_and_drop(element, target)' 'ActionChains(browser).move_to_element(element).click(target).perform()' But all i got is : 'selenium.common.exceptions.NoSuchElementException:' Thanks, Commented Jul 8, 2014 at 6:16

3 Answers 3

1

One way to do it is by clicking the 'select' element. This will 'open' the drop-down and make all the drop-down options visible to our driver. Now we will need to click the desired element.

For example, lets take a look at the following html (it's very similar to the html you provided) I took it from http://www.tizag.com/htmlT/htmlselect.php:

<select name="selectionField"> 
  <option value="CA">California -- CA </option>
  <option value="CO">Colorado -- CO</option>
  <option value="CN">Connecticut -- CN</option>
</select>

For this example I will use xpaths. Lets say I have the xpath of the 'select' element:

xpath = '/html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[4]/select'

I would like to select the option "Connecticut -- CN"

One way to do it is this:

from selenium import webdriver

driver  = webdriver.Firefox()

# navigate to the page that contains the html I provided  
driver.get('http://www.tizag.com/htmlT/htmlselect.php')

# the xpath of the <select> elemnt
xpath = '/html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[4]/select'

# click on the <select> element to open the dropdown
driver.find_element_by_xpath(xpath).click()

# select the desired option
driver.find_element_by_xpath(xpath+'/*[contains(text(), "Connecticut -- CN")]').click()
Sign up to request clarification or add additional context in comments.

1 Comment

XPaths are brittle if you are working with multiple browsers since the XPaths would be different. I've had more success with driver.find_element_by_link_text("YourSelectionHere") when working with options in a select element.
0

I would suggest you use the Select() class as it exists to handle select elements.

from selenium.webdriver.support.select import Select

select_element = Select(driver.find_element_by_name("issuer"))
select_element.select_by_visible_text("stefanSpiel")

Comments

0

Thanks all... Tried as below and it works... not sure to what extent it is good:

element = browser.find_element_by_name("issuer")
element.send_keys('stefanSpiel' + Keys.RETURN)

Thanks again :)

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.