如何使用 Selenium WebDriver 和 WebElement 从层次结构中获取价值?网页应用程序是 Telerik 模型。
我的java代码的一部分:
public void ChangeElementSaveAndVerify() throws InterruptedException
{
driver.findElement(By.xpath(myPrinterTab.printerTabIconLeftPane)).click();
Thread.sleep(5000);
driver.findElement(By.xpath(myPrinterTab.showHideDriverDetailed)).click();
Thread.sleep(5000);
WebElement select = driver.findElement(By.id("ctl00_ContentPlaceHolder2_RadGrid_Driver_ctl00_ctl05_EditFormControl_Preton_ucSettings_Driver_treeList_ctl04_Enabled_DropDown"));
WebElement innerElemt = select.findElement(By.tagName("div"));
WebElement innerElemt2 = innerElemt.findElement(By.tagName("ul"));
List<WebElement> allOptions = innerElemt2.findElements(By.tagName("li"));
for (WebElement option : allOptions) {
System.out.println("Value is: "+ option.getText());
System.out.println(option.getAttribute("value"));
driver.findElement(By.xpath(myPrinterTab.arrowUsePreton)).click();
Thread.sleep(5000);
option.click();
Thread.sleep(5000);
driver.findElement(By.xpath(myPrinterTab.saveButton)).click();
}
页面来源
<div class="rcbSlide" style="z-index: 6000; visibility: visible; display: block; overflow: visible; margin-left: 0px; position: absolute; top: 328px; left: 1122.5px; height: 42px; width: 370px;">
<div id="ctl00_ContentPlaceHolder2_RadGrid_Driver_ctl00_ctl05_EditFormControl_Preton_ucSettings_Driver_treeList_ctl08_GraphicMode_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Default " style="display: block; visibility: visible; top: 0px; left: 0px; width: 368px;">
<div class="rcbScroll rcbWidth" style="width: 100%; overflow: auto; height: 40px;">
<ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
<li class="rcbItem "> … </li>
<li class="rcbHovered "> … </li>
</ul>
</div>
</div>
</div>