我试图遍历下拉菜单中的选项或项目,但我不知道项目的数量,因为每次都会更改。
这是sList3的html部分
<select name="ctl00$ContentPlaceHolder1$Dschool" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$Dschool\',\'\')', 0)" id="ContentPlaceHolder1_Dschool" style="font-size:12pt;font-weight:bold;width:500px;">
<option selected="selected" value="0"> Select From Menu </option>
<option value="311223">first option</option>
<option value="311625">some option</option>
</select>
这是我已经开始的代码
For i = 1 To 4
Set sList1 = .FindElementById("ContentPlaceHolder1_Dedara").AsSelect
sList1.SelectByIndex i
.Wait 2000
Set sList2 = .FindElementById("ContentPlaceHolder1_Drel").AsSelect
sList2.SelectByIndex 1
.Wait 2000
Set sList3 = .FindElementById("ContentPlaceHolder1_Dschool").AsSelect
'How can I loop through the options (unknown in length)
Next i
我想循环每个选项并进行调试。打印选项的值。
在JeffC的帮助下,这是最终的解决方案
For j = 1 To sList3.Options.Count
Debug.Print sList3.Options(j).Text
Next j
答案 0 :(得分:2)
我认为您可以为每个循环使用
WebElement selectElement = driver.findElement(By.xpath("//select[@id='ContentPlaceHolder1_Dschool']"));
Select select = new Select(selectElement);
List<WebElement> options = select.getOptions();
for (WebElement we : options)
{
System.out.println("Element="+we.getText());
}
答案 1 :(得分:1)
您可以使用CSS选择器来获取父ID下的选项集合
Dim elements As Object, element As Object
Set elements = driver.FindElementsByCss("#ContentPlaceHolder1_Dschool option")
然后循环
For Each element In elements:
Debug.Print element.text
Next
CSS:
如果id为动态,则将CSS切换为
[id^=ContentPlaceHolder1] option