2016-08-25 7 views
1

私はいくつかの特定の条件でいくつかの困難を抱えています。divが隠されているときに要素を見つけることができません。

エラーがない場合、ページは次のようになります。

​​

エラーが発生した場合は、さらに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.IsDisplayed()){ 
throw (new IOException("file not found")); 

    } 

return initialize(driver, FileUpload.class); 

を試してみましたが、それが表示の有効と無効な入力

の両方のために例外をスローし、次のコード

@FindBy(xpath = "//div[@class='pmo_warning']") 
private WebElement errorMessage; 
if (errorMessage !=null){ 
throw (new IOException("file not found")); 

    } 

return initialize(driver, FileUpload.class); 

を書いた:

Unable to loケイト要素

あなたが取得する @FindAllを使用してみてください

答えて

1

代わりに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); 
+0

ありがとうございます。唯一の問題は、Find all wait timeを増やすことです。 – user2410266

+0

FindByを1つしか持たないFindAllアノテーションが必要なのはなぜですか? FindAllは、リストに記載されているすべてのFindByのOR演算子を返します。これを見てください - http://stackoverflow.com/questions/25914156/difference-between-findall-and-findbys-annotations-in-webdriver-page-factory。 FindAllを削除して、リストの宣言を単一のFindByアノテーションで使用することができます。 – Grasshopper

+0

@Grasshopper FindByは単一のWebElementを返します。FindAllByを '@FindAllBy(how = How.CSS、=" div.pmo_warning "を使用して)として使用することができます。 errorMessage' ..ありがとうございます。:) –

3

driver.findElements(By.xpath( "// divの[@クラス= 'pmo_warning']"))。サイズ()!= 0

関連する問題