WebDriver - 使用反斜杠选择选项

时间:2016-04-29 12:00:33

标签: python selenium-webdriver

我有相同的HTML代码:

<select>
<option value="domain\00031477">Data 1</option>
<option value="domain\00031478">Data 2</option>
</select>

我想按值选择选项:

select.select_by_value(value)

但是webdriver没找到这个。 我试过双反斜杠,Keys.SEPARATOR。

更新

    def set_select_by_value(self, locator, value):
        element = self.find_element(*locator)
        select = Select(element)
        select.select_by_value(value)

我致电:

page.set_select_by_value((By.ID, "SomeId"), "domain\\00031478")

堆栈跟踪:

self = <selenium.webdriver.support.select.Select instance at 0x03120468>
value = 'domain\\00031478'
def select_by_value(self, value):
"""Select all options that have a value matching the argument. That is, when given "foo" this
would select an option like:

<option value="foo">Bar</option>

:Args:
- value - The value to match against

throws NoSuchElementException If there is no option with specisied value in SELECT
"""
css = "option[value =%s]" % self._escapeString(value)
opts = self._el.find_elements(By.CSS_SELECTOR, css)
matched = False
for opt in opts:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True
if not matched:
>           raise NoSuchElementException("Cannot locate option with value: %s" % value)
E           NoSuchElementException: Message: Cannot locate option with value: domain\00031478  

1 个答案:

答案 0 :(得分:0)

尝试将您的python调用编辑为:

page.set_select_by_value((By.ID, "SomeId"), r"domain\\00031478".strip())