我有以下示例,其中字段將顯示或不顯示,具體取決於您選擇的報告。使用Selenium WebDriver查找具有動態ID的字段c#
在硒代碼字段已被定義爲:
By Field1 = By.Id("ctl00_MainContent_cntrlDynamicField1");
By Field2 = By.Id("ctl00_MainContent_cntrlDynamicField2");
By Field3 = By.Id("ctl00_MainContent_cntrlDynamicField3");
HTML:
<table>
<tbody>
<tr>
<td>
<span>Field1</span>
</td>
<td>
<select id="ctl00_MainContent_cntrlDynamicField1"
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
</tr>
<tr>
<td>
<span>Field2</span>
</td>
<td>
<select id="ctl00_MainContent_cntrlDynamicField2"
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
</tr>
<tr>
<td>
<span>Field3</span>
</td>
<td>
<select id="ctl00_MainContent_cntrlDynamicField3"
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
</tr>
</tbody>
</table>
如果我選擇(報告1)
1) Then all Fields are displayed
2) Field 1: (id="ctl00_MainContent_cntrlDynamicField1")
3) Field 2: (id="ctl00_MainContent_cntrlDynamicField2")
4) Field 3: (id="ctl00_MainContent_cntrlDynamicField3")
如果我選擇(報告3)
1) Then Field 2 is removed (id="ctl00_MainContent_cntrlDynamicField2")
2) As only 2 Fields are now displayed on the screen the ID's are now as follows
3) Field 1: (id="ctl00_MainContent_cntrlDynamicField1")
4) Field 3: (id="ctl00_MainContent_cntrlDynamicField2")
自動化將始終失敗,因爲我想使用字段3但ID已更改。
硒腳本是從xls表單驅動的數據。
如何繼續查找Field3,即使通過ID已動態更改。