简体   繁体   English

如何使用Selenium Webdriver通过两种不同的处理方式获得单个输出?

[英]How to get a single output by 2 different way of the process using selenium webdriver?

I am working on selenium and TestNG with java. 我正在使用Java进行硒和TestNG。 I have some problem about how to handle the popup. 我有一些关于如何处理弹出窗口的问题。 In here, I have 2 process at a time. 在这里,我一次有2个过程。 if I click on the button and it has an ingredient, then it will open a popup, otherwise it will add in a cart directly. 如果我单击按钮并且它有一种成分,那么它将打开一个弹出窗口,否则它将直接添加到购物车中。 So i used: 所以我用了:

@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.

So Kindly anyone make a code and share it with me. 因此,请任何人编写代码并与我分享。

Here the issue is to identify the scenario correctly. 这里的问题是正确识别方案。 Just find the existence of the expected element in case of that particular scenario else otherwise. 在这种情况下,只要找到期望元素的存在,否则就可以了。

For example, consider after clicking a button you might have two scenario's. 例如,考虑单击按钮后,您可能有两种情况。 Scenario A has element A and scenario B has element B then, 方案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
}

Modify the logic as per your logic but the trick is in findelements. 根据您的逻辑修改逻辑,但诀窍在于findelements。 You can use this to check the presence of an element easily. 您可以使用它轻松检查元素的存在。

Hope this helps. 希望这可以帮助。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM