Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SeleniumWebDriver通过两种不同的处理方式获得单个输出?_Java_Selenium - Fatal编程技术网

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

Java 如何使用SeleniumWebDriver通过两种不同的处理方式获得单个输出?,java,selenium,Java,Selenium,我正在使用java开发selenium和TestNG。我有一些关于如何处理弹出窗口的问题。在这里,我一次有两个进程。如果我点击按钮,它有一个成分,那么它将打开一个弹出窗口,否则它将直接添加到购物车。所以我用了: @Test public void ingredient_Popup() { String ingre_Title="Choose product choices..."; String ingre_Title1=d.f

我正在使用java开发selenium和TestNG。我有一些关于如何处理弹出窗口的问题。在这里,我一次有两个进程。如果我点击按钮,它有一个成分,那么它将打开一个弹出窗口,否则它将直接添加到购物车。所以我用了:

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

因此,请任何人编写一个代码并与我共享。

这里的问题是正确识别场景。在特定的场景中,只需找到预期元素的存在,否则就不存在了

例如,在单击一个按钮后,您可能会有两个场景。场景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。您可以使用它轻松地检查元素的存在

希望这有帮助。 谢谢