I suggest using the Select()
class.
from selenium.webdriver.support.select import Select
select = Select(driver.find_element_by_id("id_category"))
select.select_by_visible_text("Test1")
Frage
My drop down list:
<select id="id_category" name="category">
<option value="" selected="selected">---------</option>
<option value="1">Category #1</option>
<option value="2">Test1</option>
</select>
I wanted to tell selenium to choose Test1, i tried every option i found in the Internet, but it just doesn't work..
Here are examples i tried:
#first example
self.browser.find_element_by_xpath("//select[@name='id_category']/option[text()='Test1']").click()
#second example
select_category = driver.find_element_by_id('id_category') for option in select_category.find_elements_by_tag_name('option'):
if option.text == 'Test1':option.click()
Im not sure, but i think that Selenuim see only first option, this one with (selected="selected").
This i error msg i got:
ERROR: test_create_blog_entry_admin (tests.AdminTest)
Test for adding blog entry
----------------------------------------------------------------------
Traceback (most recent call last):
File "F:\projekty\careerguide\careerguide\careerguide\ft\tests.py", line 66, in test_create_blog_entry_admin self.browser.find_element_by_xpath("//select[@id='id_category']/option[text(
)='Test1']").click()
File "F:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 223, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "F:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 655, in find_element
{'using': by, 'value': value})['value']
File "F:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 166, in execute
self.error_handler.check_response(response)
File "F:\Python27\lib\site-packages\selenium-2.41.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//select[@id=\'id_category\']/option[text()=\'Test1\']"}' ; Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/konrad/appdata/local/temp/tmpfxvjan/extensions/fxdriver@googlecode.com/components/driver_component.js:8905)
at FirefoxDriver.prototype.findElement (file:///c:/users/konrad/appdata/local/temp/tmpfxvjan/extensions/fxdriver@googlecode.com/components/driver_component.js:8914)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/konrad/appdata/local/temp/tmpfxvjan/extensions/fxdriver@googlecode.com/components/command_processor.js:10884)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/konrad/appdata/local/temp/tmpfxvjan/extensions/fxdriver@googlecode.com/components/command_processor.js:10889)
at DelayedCommand.prototype.execute/< (file:///c:/users/konrad/appdata/local/temp/tmpfxvjan/extensions/fxdriver@googlecode.com/components/command_processor.js:10831)
Lösung
I suggest using the Select()
class.
from selenium.webdriver.support.select import Select
select = Select(driver.find_element_by_id("id_category"))
select.select_by_visible_text("Test1")
Andere Tipps
In first example you should use id instead of name
self.browser.find_element_by_xpath("//select[@id='id_category']/option[text()='Test1']").click()
Couldnt find any issue in second example. try option.select()
. It was there in earlier versions of webdriver. Not sure which one you are using.