我无法从下拉id值中选择项目是动态webdriver

时间:2014-06-15 18:40:49

标签: c# xpath webdriver

我正在使用Webdriver C#,表单上有一个下拉列表。它的 id 值是动态的

id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52311_3_52311_3_DDL" 

每次打开表单时,数字:52311都会更改。 如何在下拉列表中选择项目?

我尝试了以下代码 - 我想在下拉列表中选择Switzerland值: 谢谢,

IWebElement dropDownListBox = 


<form id="aspnetForm" enctype="multipart/form-data" action="/33985/quality-assurance-committee/472/application" method="post" name="aspnetForm">
<input id="__EVENTTARGET" type="hidden" value="" name="__EVENTTARGET"/>
<input id="__EVENTARGUMENT" type="hidden" value="" name="__EVENTARGUMENT"/>
<input id="__VIEWSTATE" type="hidden"     value="/wEPDkPz/fw==" name="__VIEWSTATE"/>
<input id="__EVENTVALIDATION" type="hidden" value="/wEWOfc=" name="__EVENTVALIDATION"/>
<div/>
<script type="text/javascript"> //<![CDATA[ var theForm = document.forms['aspnetForm']; some javascript code here </script>
<div class=""/>
<div id="ctl00_uxFormTemplate" question="" gotoarray="">
<nav class="navbar navbar-static-top navbar-default" container="false"  role="navigation">
<div id="page-container" class="container">
<div class="">
<div>
<div id="ctl00_uxFormTemplate_uxErrorControl" class="container" question="" gotoarray=""/>
<div class="container">
<div id="content-container" class="col-md-12">
<div id="ctl00_uxFormTemplate_uxBreadCrumbNavigationControl" class="breadcrumb- container col-md-12 nopadding" question="" gotoarray="">
<div class="form-horizontal">
<div id="ctl00_uxFormTemplate_uxApplicationControl" question="" gotoarray="">
<h1 class="borderbottom">Question</h1>
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl" class="questions-container" question="" gotoarray="">
<span/>
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52309_1" class="question-control form-group" question="52309" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52310_2" class="question-control form-group" question="52310" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52311_3" class="question-control form-group" question="52311" gotoarray="{}" style="display: block;">
<div class="col-md-5">
<div class="col-md-7">
<select id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52311_3_52311_3_DDL" class="form-control" name="ctl00$uxFormTemplate$uxApplicationControl$uxQuestionControl$52311_3$52311_3_DDL">
</div>
</div>
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52312_4" class="textbox question-control form-group" question="52312" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52313_5" class="image-container image-container question-control form-group" question="52313" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52314_6" class="form-group question-control" question="52314" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52315_7" class="question-control form-group" question="52315" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52316_9" class="question-control form-group text" question="52316" gotoarray="{}" style="display: block;">
<div id="ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_52317_11" class="form-group question-control" question="52317" gotoarray="{}" style="display: block;">
<input id="error-count" type="hidden" value="0"/>
</div>
<input id="error-count" type="hidden" value="0"/>
</div>
</div>
<div id="ctl00_uxFormTemplate_uxNavigationControl" class="col-md-12 nopadding" question="" gotoarray="">
</div>
</div>
</div>
<div class="col-md-12"/>
<div class="col-md-6">
<div class="col-md-6">
</div>
</div>

1 个答案:

答案 0 :(得分:0)

这应该适合你:

IWebElement selectElement = driver.FindElement(By.CssSelector("select[id^='ctl00_uxFormTemplate_uxApplicationControl_uxQuestionControl_']"));

SelectElement select = new SelectElement(selectElement);
select.SelectByText("Switzerland");