0

Any idea how to select an option from a element defined like so:

<select onchange="onIsIsNot('COND_MT_USER_MOB_ACTIVE', this)">
<option selected="" value="512">Cellular</option>
<option value="256">WLAN</option>
<option value="768">Cellular or WLAN</option>
</select>

I'm using Python and tried variations of the following:

select = "COND_MT_USER_MOB_ACTIVE"
value = "Cellular"
sel.select("css=select[onchange='onIsIsNot(\'%s\', this)']"%select, "label=%s"%value)

I think I'm not getting the quotes within quotes within quotes correct for the onchange attribute.

paul
  • 23
  • 5
  • Can you change the HTML, or is that not something you have access to? 'Cause the best way to do this would be to add an ID or Class to the `select`. – Izkata May 15 '12 at 20:52
  • Also, I think `value` should be the actual value of the select, such as `256` or `512`. – Izkata May 15 '12 at 20:53
  • Izkata, thanks for the response. Regarding the value...you are correct. In my method for choosing a select item I try "label" first and then "value". I pasted the wrong line in my question. I've edited the question to correct it. Regarding changing the HTML, I pushed my page designer to add an ID and it looks like that may be the only solution. – paul May 17 '12 at 15:31

1 Answers1

0

So, if you can get an id attribute added, it ought to be easy:

sel.select("id=%s"%id_val, "label=%s"%value)

That said, if you can't do it, consider what's being escaped where:

In [1]: z = "\'"

In [2]: z
Out[2]: "'"

So in the following line:

sel.select("css=select[onchange='onIsIsNot(\'%s\', this)']"%select.......

This is what the selenium selector is seeing:

"css=select[onchange='onIsIsNot('COND_MT_USER_MOB_ACTIVE', this)']"

Note how the single quotes are no longer escaped for the CSS selector. I suspect this is what you need, although I haven't tested it (Look at the r at the beginning of the string, for a "raw string"):

sel.select(r"css=select[onchange='onIsIsNot(\'%s\', this)']"%select.......
Izkata
  • 8,961
  • 2
  • 40
  • 50