2014-11-06 6 views
10

मैं क्रोम ड्राइवर का उपयोग कर रहा हूं और वेबपृष्ठ का परीक्षण करने का प्रयास कर रहा हूं।org.openqa.selenium.UnhandledAlertException: अप्रत्याशित चेतावनी

आम तौर पर यह ठीक चलता है, लेकिन कुछ समय मैं exceptions-- हो जाता है

org.openqa.selenium.UnhandledAlertException: unexpected alert open 
(Session info: chrome=38.0.2125.111) 
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 16 milliseconds: null 
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30' 
System info: host: 'Casper-PC', ip: '10.0.0.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_25' 
Driver info: org.openqa.selenium.chrome.ChromeDriver 

तब मैं चेतावनी संभाल करने की कोशिश की -

Alert alt = driver.switchTo().alert(); 
    alt.accept(); 

लेकिन इस बार मैं org.openqa recived-- .selenium.NoAlertPresentException

मैं अलर्ट के स्क्रीन शॉट्स को जोड़ रहा हूं- First Alert and by using esc or enter i gets the second alertSecond Alert

मुझे यह पता लगाने में सक्षम नहीं है कि अब क्या करना है। समस्या यह है कि मुझे हमेशा यह अपवाद नहीं मिल रहा है। और जब ऐसा होता है तो परीक्षण विफल रहता है।

+0

क्या यह मूल कारण हो सकता है? Fxdriver.modals.clearFlag_ को एक बाईपास कॉल ... सीएफ। https: // stackoverflow।कॉम/प्रश्न/44568402/कैसे-करें-i-मैन्युअल-माउस-खारिज-ए-जावास्क्रिप्ट-अलर्ट-एंड-गेट-द-द-द-बॉडी-ओ/44592827 # 44592827 – NevilleDNZ

+0

क्या यह मूल कारण हो सकता है? Fxdriver.modals.clearFlag_ को एक बाईपास कॉल ... सीएफ। https://stackoverflow.com/questions/44568402/how-do-i-manually-mouse-dismiss-a-javascript-alert-and-get-back-the-the-body-o/44592827#44592827 – NevilleDNZ

उत्तर

13

मुझे यह समस्या भी थी। जब यह चेतावनी का सामना करता है तो ड्राइवर के डिफ़ॉल्ट व्यवहार के कारण होता था। डिफ़ॉल्ट व्यवहार "ACCEPT" पर सेट किया गया था, इस प्रकार अलर्ट स्वचालित रूप से बंद कर दिया गया था, और switchTo()। चेतावनी() इसे नहीं मिला।

DesiredCapabilities dc = new DesiredCapabilities(); 
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE); 
d = new FirefoxDriver(dc); 

तो फिर तुम इसे संभाल कर सकते हैं::

try { 
    click(myButton); 
} catch (UnhandledAlertException f) { 
    try { 
     Alert alert = driver.switchTo().alert(); 
     String alertText = alert.getText(); 
     System.out.println("Alert data: " + alertText); 
     alert.accept(); 
    } catch (NoAlertPresentException e) { 
     e.printStackTrace(); 
    } 
} 
1

क्या आपका स्विच किसी प्रयास/पकड़ ब्लॉक के भीतर अलर्ट करने के लिए है? तुम भी एक प्रतीक्षा टाइमआउट जोड़ने के लिए देखने के लिए चाहते हो सकता है अगर चेतावनी एक निश्चित देरी

try { 
    // Add a wait timeout before this statement to make 
    // sure you are not checking for the alert too soon. 
    Alert alt = driver.switchTo().alert(); 
    alt.accept(); 
} catch(NoAlertPresentException noe) { 
    // No alert found on page, proceed with test. 
} 
+0

हां मैं एक कोशिश पकड़ ब्लॉक का उपयोग कर रहा हूँ –

+0

क्या आप कॉल अलर्ट कॉल करने के लिए स्विच के आसपास कोड स्निपेट पोस्ट कर सकते हैं? – shri046

0

आप इस टुकड़ा कोशिश कर सकते हैं के बाद पता चलता है:

public void acceptAlertIfAvailable(long timeout) 
     { 
     long waitForAlert= System.currentTimeMillis() + timeout; 
     boolean boolFound = false; 
     do 
     { 
      try 
      { 
      Alert alert = this.driver.switchTo().alert(); 
      if (alert != null) 
      { 
       alert.accept(); 
       boolFound = true; 
      } 
      } 
      catch (NoAlertPresentException ex) {} 
     } while ((System.currentTimeMillis() < waitForAlert) && (!boolFound)); 
     } 
2

आप सेलेनियम WebDriver में Wait कार्यक्षमता का उपयोग कर सकते हैं प्रतीक्षा करने के लिए एक चेतावनी के लिए, और यह उपलब्ध होने के बाद इसे स्वीकार करें।

सी # में -

public static void HandleAlert(IWebDriver driver, WebDriverWait wait) 
{ 
    if (wait == null) 
    { 
     wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); 
    } 

    try 
    { 
     IAlert alert = wait.Until(drv => { 
      try 
      { 
       return drv.SwitchTo().Alert(); 
      } 
      catch (NoAlertPresentException) 
      { 
       return null; 
      } 
     }); 
     alert.Accept(); 
    } 
    catch (WebDriverTimeoutException) { /* Ignore */ } 
} 

जावा में इसके बराबर -

public static void HandleAlert(WebDriver driver, WebDriverWait wait) { 
    if (wait == null) { 
     wait = new WebDriverWait(driver, 5); 
    } 

    try { 
     Alert alert = wait.Until(new ExpectedCondition<Alert>{ 
      return new ExpectedCondition<Alert>() { 
       @Override 
       public Alert apply(WebDriver driver) { 
       try { 
        return driver.switchTo().alert(); 
       } catch (NoAlertPresentException e) { 
        return null; 
       } 
       } 
      } 
     }); 
     alert.Accept(); 
    } catch (WebDriverTimeoutException) { /* Ignore */ } 
} 

यह है, जब तक एक चेतावनी मौजूद है 5 सेकंड के लिए इंतजार करेंगे आप अपवाद को पकड़ने और, इसके साथ सौदा करता है, तो कर सकते हैं अपेक्षित चेतावनी उपलब्ध नहीं है।

0

क्लिक करें घटना के बाद

try{ 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
     } 
catch (org.openqa.selenium.UnhandledAlertException e) {     
     Alert alert = driver.switchTo().alert(); 
     String alertText = alert.getText().trim(); 
     System.out.println("Alert data: "+ alertText); 
     alert.dismiss();} 

को संभालने के लिए नीचे दिए गए कोड इस ऐड ... अन्य बातों के driver.close करना();

0

समाधान ("ध्यान न दें"), इतना है कि यह चेतावनी बंद नहीं होती है ड्राइवर के डिफ़ॉल्ट व्यवहार को संशोधित करने के लिए है

मेरे लिए काम कर रहा है

private void acceptSecurityAlert() { 

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS)   
                  .pollingEvery(3, TimeUnit.SECONDS)   
                  .ignoring(NoSuchElementException.class);  
    Alert alert = wait.until(new Function<WebDriver, Alert>() {  

     public Alert apply(WebDriver driver) { 

      try { 

       return driver.switchTo().alert(); 

      } catch(NoAlertPresentException e) { 

       return null; 
      } 
     } 
    }); 

    alert.accept(); 
} 
संबंधित मुद्दे