如何使用WebDriver从页面获取价值>>找到元素

时间:2013-08-07 13:52:07

标签: webdriver

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

2 个答案:

答案 0 :(得分:0)

findElement所获得的WebElement没有像文本值这样的属性。 尝试使用不同的By-Mechanism (By.id或By.xpath)来查找正确的WebElement。

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

答案 1 :(得分: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脚本中,我认为这个想法将有助于你