Riaz Ladhani
Riaz Ladhani

Reputation: 4072

Selenium Python I am trying to select a drop down from a nested span tag using XPATH or CSS selector

I am trying to work out the XPATH or CSS for the drop down element which seems to be in nested span tags. I want to find the tag I can find the text above the drop down but i cannot go down the span tags to get to the drop down element.

I have XPATH to get to the text above the drop down. The XPATH is:

//span[contains(text(), "Select a data preview to import configuration from")]

If i try to use preceding::span[2] it goes too far down.

//span[contains(text(), "Select a data preview to import configuration from")]/preceding::span[2]

The HTML snippet is:

<div class="GPI5XK1CM" style="padding-right: 16px;position:relative;outline:none;" __idx="0" onclick="">
<div style="position:absolute;display:none;"/>
<div>
<span/>
<span>
<span title="" style="font-weight:bold;">Select a data preview to import configuration from</span>
</span>
<span/>
<span>
<span class="" title="" style="display:block;"/>
</span>
<span/>
<span/>
<span/>
<span/>
<span/>
<span>
<span class="" title="None" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;">
    <select tabindex="-1">
        <option selected="selected" value="None">None</option>
        <option value="CRMINVALID_07102015">CRMINVALID_07102015</option>
        <option value="LADEMO_crm2_Chrome">LADEMO_crm2_Chrome</option>
        <option value="LADEMO_CRM_DONOTCHANGE_CHROME">LADEMO_CRM_DONOTCHANGE_CHROME</option>
        <option value="LADEMO_ESCR_DO_NO_CHANGE_CHROME">LADEMO_ESCR_DO_NO_CHANGE_CHROME</option>
        <option value="Lademo_odb_Data">Lademo_odb_Data</option>
        <option value="test">test</option>
    </select>
    </span>
    </span>
</div>

What XPATH or CSS could i use to get to the drop down? CSS is faster, that would be good.

I think I have worked one out now but not sure if it is a good way to do it. This one works for me:

//span[contains(text(), "Select a data preview to import configuration from")]/preceding::span[1]//../span//../select

Using //../select if the structure changes it will still work.

Is this the correct way?

Thanks, Riaz

Upvotes: 0

Views: 1272

Answers (3)

user3821521
user3821521

Reputation:

An example for CSS. This assumes only one span titled 'None'.

$$("span[title='None']>select>option[value='whateveroptionyouseek']")

You could also probably get away with:

$$("span[title='None'] option[value='whateveroptionyouseek']")

Upvotes: 0

Piyush
Piyush

Reputation: 521

you can use following xpath to get all option value:

//span[@title='None']/select/option/text()

Upvotes: 0

JeffC
JeffC

Reputation: 25744

I've got a couple CSS selectors that you can try. The first one should be enough but if not, the second one should be unique.

"select[tabIndex='-1']"
"span[title='None'] > select[tabIndex='-1']"

Upvotes: 0

Related Questions