![](/img/trans.png)
[英]How to get all options in a drop-down list by Selenium WebDriver using C#?
[英]How to sort out all values of drop down list using Selenium (C#)
有人可以幫忙嗎?我需要選擇drop_down_1,選擇第一個選項,然后自動在另一個drop_down_2中顯示帶有選項的列表,我必須在drop_down_2中選擇第一個選項,單擊“獲取結果”按鈕,一段時間后結果將顯示為刪除它,並選擇第二個選項,依此類推。 因此,我需要為drop_down_1的每個選項遍歷drop_down_2的每個選項。
下面的一些html代碼:
第一個具有很多選項的下拉列表:
<select id="manufacturer-selector" data-bind="value: selectedManufacturer">
<option value=""></option>
<option value="5637148326">Nokia</option>
<option value="5637148327">Sony</option>
<option value="5637148328">Lenovo-new</option>
....
第二個下拉列表,其中有很多選項:
<select id="product-selector" data-bind="attr: { disabled: countManufacturerProducts() === 0 }, foreach: manufacturerProducts, value: selectedProduct">
<option data-bind="text: Name, attr: { value: ID }" value="5637191581">390</option>
<option data-bind="text: Name, attr: { value: ID }" value="5637201719">390 Screenphone</option>
<option data-bind="text: Name, attr: { value: ID }" value="5637206334">470 Screenphone</option>47
我是否需要創建一個循環並在其中再循環一個?
首先-使用selenium.support.ui
創建2個選擇元素
SelectElement select1 = driver.FindElement(By....);
SelectElement select2 = driver.FindElement(By....);
然后計算選擇1中有多少“選項”元素。
var optionCounter1 = driver.FindElements(By...).Count;
var optionCounter2 = driver.FindElements(By...).Count;
for (int currOption1 =1; currOption1 < optionCounter; currOption1 ++)
{
select1.SelectByIndex(currOption1 );
for (int currOption2 =1; currOption2 < optionCounter; currOption2 ++)
{
select1.SelectByIndex(currOption1);
select2.SelectByIndex(currOption2);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.