我的下拉列表:
<select id="id_category" name="category">
<option value="" selected="selected">---------</option>
<option value="1">Category #1</option>
<option value="2">Test1</option>
</select>
我想告诉selenium选择Test1,我尝试了我在互联网上找到的每一个选项,但它只是不起作用..
以下是我尝试过的例子:
#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()
我不确定,但我认为Selenuim只看到第一个选项,这个选项(选择=&#34;选择&#34;)。
这是我的错误信息我得到了:
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)
答案 0 :(得分:2)
我建议使用Select()
类。
from selenium.webdriver.support.select import Select
select = Select(driver.find_element_by_id("id_category"))
select.select_by_visible_text("Test1")
答案 1 :(得分:1)
在第一个示例中,您应该使用id而不是名称
self.browser.find_element_by_xpath("//select[@id='id_category']/option[text()='Test1']").click()
在第二个示例中无法找到任何问题。试试option.select()
。它存在于早期版本的webdriver中。不确定你使用的是哪一个。