'Python lxml - get selected options

I'm trying to access selected options in select using lxml but looks like lxml doesn't know about this property. Here is my code:

for option in productField.xpath('select//option'):
    if 'selected' in option.attrib:
        print(option)

When I do print(option.attrib) I see only {'value': 'value...'}. Am I wrong somewhere?



Solution 1:[1]

Got it.

if (productField.xpath('select//option[@selected="selected"]')):
    for option in productField.xpath('select//option[@selected="selected"]'):
        print(option)

Solution 2:[2]

You can use selected option:

productField.xpath("//select/option[@selected='selected']/text()")[0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vito Gravano
Solution 2 ??????? ?????????