首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在隐藏div时无法定位元素

在隐藏div时无法定位元素
EN

Stack Overflow用户
提问于 2016-08-25 15:50:49
回答 2查看 517关注 0票数 1

我对某些具体的情况有一些困难。

当没有错误时,页面如下所示。

代码语言:javascript
复制
<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类。

代码语言:javascript
复制
<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">

我想验证我是否上传了一个无效的文件并显示错误,抛出一个异常,否则继续。

我写了以下代码

代码语言:javascript
复制
@FindBy(xpath = "//div[@class='pmo_warning']")
private WebElement errorMessage;
if (errorMessage !=null ){
throw (new IOException("file not found"));

    }

return initialize(driver, FileUpload.class);

它为有效和无效的输入引发异常。

我也试过

代码语言:javascript
复制
@FindBy(xpath = "//div[@class='pmo_warning']")
private WebElement errorMessage;
if (errorMessage.IsDisplayed()){
throw (new IOException("file not found"));

    }

return initialize(driver, FileUpload.class);

对于没有错误的文件,它显示:

无法定位元素

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-25 16:09:23

您应该尝试使用@FindAll来获取WebElement列表,并检查其大小如下:

代码语言:javascript
复制
@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);
票数 1
EN

Stack Overflow用户

发布于 2016-08-25 15:57:33

driver.findElements(By.xpath("//div@class='pmo_warning'")).size() != 0

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39149690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档