Shuffler
Shuffler

Reputation: 173

Selenium select dropdown element using xpath string

I have quite specific problem regarding both selenium and xpath.

I have to make an automated tests based on scenarios using selenium. All pages are automatically generated and using Ids is impossible.

All form elements though are designed in same way.

 <table>
 <tr><td> Title </td></tr>
 <tr><td> input/dropdown/etc </td></tr>
 </table>

He is the specifics

<tr>
<td width="34%" valign="top" bgcolor="#ffffc7">
<span class="bold">Status wniosku</span>
<span>Test</span>
</td>
<td width="66%" bgcolor="#ffffc7">
<select id="ctl00_ContentPlaceHolder_2041" class="baseCtrl" name="ctl00$ContentPlaceHolder$2041">
<option value="" selected="selected">- wybierz -</option>
<option value="save">tylko zapisz</option>
<option value="pj">zapisz i wyślij do PJ</option>
</select>
<span>
</span>
<span id="ctl00_ContentPlaceHolder_ctl19" class="validation" style="display:none;">Określ status wniosku</span>
<span id="ctl00_ContentPlaceHolder_ctl20" class="validation" style="display:none;"></span>
<span></span>
</td>
</tr>

Using http://www.xmlme.com/XpathTool.aspx I have designed xpath for dropdown elements.

//span[text()='LABELNAME']/ancestor::*[1]/following-sibling::*/select/option[text()='TEXTVALUE']

I would like to use Selenium to click on the element i found. I've tried Selenium.Click() and variations of Selenium.Select() but with no results.

My question is, is the xpath designed correctly? If so how should i execute it using Selenium? Thx for the help.

Upvotes: 1

Views: 5825

Answers (1)

Santoshsarma
Santoshsarma

Reputation: 5667

Try below xpath for selecting second option

"//span[text()='Test']/ancestor::*[1]/following-sibling::*/selec‌​t"

Ex:

Selenium.Select("//span[text()='Test']/ancestor::*[1]/following-sibling::*/selec‌​t","label=Save");

Upvotes: 1

Related Questions