我对某些具体的情况有一些困难。
当没有错误时,页面如下所示。
<script src="includes/js/errorHandling.js?v=5" type="text/javascript"/>
<div class="pmo_title">
<!--end pmo title-->
<div class="spacer2"/>
<div class="pmo-container">但是,如果发生任何错误,则显示额外的div类。
<script src="includes/js/errorHandling.js?v=5" type="text/javascript"/>
<div class="pmo_title">
<!--end pmo title-->
<div class="pmo_warning">
<div class="pmo-container">
<span class="message_title">Errors :</span>
<!--display first item of list with no comma-->
<span id="fileError" class="error">File to Upload required</span>
</div>
</div>
<div class="spacer2"/>
<div class="pmo-container">我想验证我是否上传了一个无效的文件并显示错误,抛出一个异常,否则继续。
我写了以下代码
@FindBy(xpath = "//div[@class='pmo_warning']")
private WebElement errorMessage;
if (errorMessage !=null ){
throw (new IOException("file not found"));
}
return initialize(driver, FileUpload.class);它为有效和无效的输入引发异常。
我也试过
@FindBy(xpath = "//div[@class='pmo_warning']")
private WebElement errorMessage;
if (errorMessage.IsDisplayed()){
throw (new IOException("file not found"));
}
return initialize(driver, FileUpload.class);对于没有错误的文件,它显示:
无法定位元素
发布于 2016-08-25 16:09:23
您应该尝试使用@FindAll来获取WebElement列表,并检查其大小如下:
@FindAll(@FindBy(how = How.CSS, using = "div.pmo_warning"))
List<WebElement> errorMessage;
if (errorMessage.size() > 0 && errorMessage.get(0).isDisplayed()){
throw (new IOException("file not found"));
}
return initialize(driver, FileUpload.class);发布于 2016-08-25 15:57:33
driver.findElements(By.xpath("//div@class='pmo_warning'")).size() != 0
https://stackoverflow.com/questions/39149690
复制相似问题