尝试使用driver.findElement时,没有这样的元素异常(By

时间:2015-03-06 18:28:15

标签: selenium remotewebdriver

我的代码看起来像(在java中),在chrome上运行:

@Test
     public void verifyRedAlertCountDisplayed() throws Exception {              
         driver.findElement(By.cssSelector(this.RED_ALERTS_BTN));
}

我宣布RED_ALERTS_BTN:

public final String RED_ALERTS_BTN = "#divAOISummary > div > div.list-summary.list-summary-red.active";

我还尝试过简单地使用id:

@Test
     public void verifyRedAlertCountDisplayed() throws Exception {              
         driver.findElement(By.id(this.RED_ALERTS_BTN));
}

public final String RED_ALERTS_BTN = "divAOISummary";

似乎尝试了一切(即使使用xpath),我仍然得到了“没有这样的元素”#39; ;

实际的HTML:

<div id="pnlRightDB-targetEl" style="position: absolute; width: 320px; left: 0px; top: 0px; height: 1px;"><div class="x-panel x-box-item x-panel-default" id="pnlAOIHeader" style="left: 0px; margin: 0px; width: 320px; top: 0px; height: 215px;"><div id="pnlAOIHeader-body" class="x-panel-body x-panel-body-default x-panel-body-default" style="width: 320px; left: 0px; top: 0px; height: 215px;"><div id="pnlAOIHeader-clearEl" class="x-clear" role="presentation"></div><div id="pnlAOIHeader_Content" class="">  
                                <div id="divAOIHeader" class="clearfix" style="height: 213px;">
        <div class="panel-title">Incidents<span class="icon-refresh" title="Refresh the Incident List" onclick="refreshIncidentList()"></span></div>
        <div id="divAOISummary" class="IncidentSummary"><div class="list-summary-holder">
                <div class="list-summary list-summary-red active" title="1 Red Alert">1</div>
                <div class="list-summary list-summary-yellow" title="No Yellow Alerts">0</div>
                <div class="list-summary list-summary-green" title="No Green Alerts">0</div>
        </div>      
</div>

任何人都可以帮忙或给我一个想法?似乎所有其他测试用例使用类似的技术运行得很好;
非常感谢任何建议! aluchia

1 个答案:

答案 0 :(得分:1)

我会尝试使用explicit等待该元素,直到找到它为止。

@Test
public void verifyRedAlertCountDisplayed() throws Exception {              
         By byCss = By.cssSelector("byCss");

         WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byCss));

}