2017-05-31 9 views
0

私はセレニウムとTestNGにjavaを使って作業しています。私はポップアップを処理する方法についていくつかの問題があります。ここでは、一度に2つのプロセスがあります。ボタンをクリックすると成分がある場合はポップアップが開き、それ以外の場合はカートに直接追加されます。だから私は使用:selenium webdriverを使用して2つの異なる方法で1つの出力を取得するにはどうすればよいですか?

@Test   
public void ingredient_Popup()  
{ 
    String ingre_Title="Choose product choices..."; 
     String ingre_Title1=d.findElement(By.className("modal-dialog")).getText(); 
    if(ingre_Title.contentEquals(ingre_Title1))   
     {  
     d.findElement(By.id("ingredient_quantity")).sendKeys("1"); 
     d.findElement(By.linkText("SUBMIT")).click();  
    }   
    else  
    {  
d.findElement(By.xpath("/html/body/section[3]/div[2]/div/div/div/div/div/div[1]/div[3]/div/div/div[3]/div/div[1]/table/tbody/tr/td[2]/div/span[2]/button")).click();   
}  
And also i used (II)  
@Test   
public void ingredient_Popup()WebElement element;  
try  
{  
    element = d.findElement(By.className("modal-dialog")); >}    
catch(NoSuchElementException n)   
{ 
    element = null; 
    }  
{  
    if(element !=null)   
{    
     d.findElement(By.id("ingredient_quantity")).sendKeys("1");    
d.findElement(By.linkText("SUBMIT")).click();  
    }   
else   
{  
     d.findElement(By.className("btn btn-default btn-number")).click();   
    }   
}  
And i used, isenabled, Contains, equals, isdisplayed, isElementPresent   
if(ingre_Title.isEnabled())   
    {  
    d.findElement(By.id("ingredient_quantity")).sendKeys("1");   
d.findElement(By.linkText("SUBMIT")).click();  
    }  
    else  
    {  
     d.findElement(By.xpath("/html/body/section[3]/div[2]/div/div/div/div/div/div[1]/div[3]/div/div/div[3]/div/div[1]/table/tbody/tr/td[2]/div/span[2]/button")).click();  
}   
And i tried a lot, Nothing is working. i'm getting NoSuchElementException  
error. 

だから、親切に、誰もがコードを作成し、私と一緒にそれを共有します。

答えて

0

ここで問題はシナリオを正しく識別することです。特定のシナリオの場合には、それ以外の場合は期待される要素の存在を見つけてください。

たとえば、ボタンをクリックした後に2つのシナリオがあるとします。シナリオAは、要素AとシナリオBは、その要素Bを持っていた、

driver.findelement(By.xpath("button")).click; 

//Identifying the scenario by checking the presence of the element 
if(driver.findElements(By.xpath("A")).size>0) 
{ 
    //IF THIS IS TRUE NOW YOU ARE IN SCENARIO A 
    //DO YOUR STUFF ACCORDING TO SCENARIO A 
} 
else if(driver.findElements(By.xpath("B")).size>0) 
{ 
    //IF THIS IS TRUE NOW YOU ARE IN SCENARIO B 
    //DO YOUR STUFF INCASE OF SCENARIO B 
} 

は、あなたのロジックに従ってロジックを変更しますが、トリックはfindelementsです。これを使用して、要素の存在を簡単にチェックすることができます。

これが役に立ちます。おかげさまで

+0

@Stepi Kzyalがこれはあなたのために働いていますか? –

関連する問題