0

如何使用 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>
4

2 回答 2

0

您获得的 WebElementfindElement没有像文本值这样的属性。尝试使用不同的 By-Mechanism (By.id or By.xpath)找到正确的 WebElement 。

如果这不起作用,请发布 HTML,以便更容易提供帮助。

于 2013-08-07T14:06:13.683 回答
0
options=driver.find_element_by_class_name("the class name in which u r text monitor is present")
for option in options:
temp=option.text
print temp

这是在 python 脚本中,我认为这个想法会帮助你

于 2013-12-14T11:21:17.250 回答