Java 网络驱动程序:为什么我仍然得到';意外警报打开';?

Java 网络驱动程序:为什么我仍然得到';意外警报打开';?,java,selenium,jenkins,selenium-webdriver,webdriver,Java,Selenium,Jenkins,Selenium Webdriver,Webdriver,我创建了以下方法,单击特定按钮将打开警报,下面列出的相同方法将处理警报 但我似乎得到了奇怪的詹金斯构建,它将(间歇性)失败,因为意外警报打开(下面列出的例外),有什么想法吗 public void clickPaySecurelyNowButton() throws InterruptedException { waitAndclickElementUsingJS(button_PaySecurelyNowTop); WebDriverWait tempWait

我创建了以下方法,单击特定按钮将打开警报,下面列出的相同方法将处理警报

但我似乎得到了奇怪的詹金斯构建,它将(间歇性)失败,因为意外警报打开(下面列出的例外),有什么想法吗

public void clickPaySecurelyNowButton() throws InterruptedException {
        waitAndclickElementUsingJS(button_PaySecurelyNowTop);

        WebDriverWait tempWait = new WebDriverWait(this.driver, 30);
        Alert alert = tempWait.until(ExpectedConditions.alertIsPresent());
        boolean boolAlert = false;
        int attempts = 0;
        while (!boolAlert && attempts < 1000) {
            try {
                driver.switchTo().alert().accept();
                System.out.println("Successfully clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
                boolAlert = true;
            } catch (org.openqa.selenium.UnhandledAlertException e) {
                driver.switchTo().alert().accept();
                boolAlert = true;
                System.out.println("Sucesffuly clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
            } catch (Exception e) {
                System.out.println("Unable to click on the 'Pay Securely Now Button', Exception: " + e.getMessage());
                Assert.fail("Method failed: clickPaySecurelyNowButton");
            }
        } attempts++;
    }
public void clickPaySecurelyNowButton()抛出InterruptedException{
等待并单击使用JS的元素(按钮\u pay securelynowtop);
WebDriverWait tempWait=新的WebDriverWait(this.driver,30);
Alert Alert=tempWait.until(ExpectedConditions.alertIsPresent());
布尔布尔布尔布尔勒特=假;
int=0;
而(!boolAlert&&truments<1000){
试一试{
driver.switchTo().alert().accept();
System.out.println(“成功单击“立即安全支付按钮”并“关闭弹出窗口”);
boolAlert=true;
}catch(org.openqa.selenium.unhandledalertexe){
driver.switchTo().alert().accept();
boolAlert=true;
System.out.println(“成功单击“立即安全支付按钮”并“关闭弹出窗口”);
}捕获(例外e){
System.out.println(“无法单击“立即安全支付”按钮”,异常:+e.getMessage());
Assert.fail(“方法失败:单击PaySecureyNowButton”);
}
}尝试++;
}
标记的异常:

unexpected alert open
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 63 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 699ee1257882397cd7695aeaf919c68f
*** Element info: {Using=xpath, value=//button[contains(text(), "Pay Securely Now")]}



org.openqa.selenium.UnhandledAlertException: 
unexpected alert open
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 63 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 699ee1257882397cd7695aeaf919c68f
*** Element info: {Using=xpath, value=//button[contains(text(), "Pay Securely Now")]}
    at BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest.clickOn_PaySecurelyNowButton(AddingDrivingExperienceToBasketTest.java:51)






TEST CLASS END TIME: 03.10.30
0
0
Tests run: 314, Failures: 80, Errors: 0, Skipped: 214, Time elapsed: 62.972 sec <<< FAILURE! - in TestSuite
clickOn_PaySecurelyNowButton(BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest)  Time elapsed: 0.298 sec  <<< FAILURE!
org.openqa.selenium.UnhandledAlertException: 
unexpected alert open
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 63 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 699ee1257882397cd7695aeaf919c68f
*** Element info: {Using=xpath, value=//button[contains(text(), "Pay Securely Now")]}
        at BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest.clickOn_PaySecurelyNowButton(AddingDrivingExperienceToBasketTest.java:51)

EmailFormatAndDataCapture(BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest)  Time elapsed: 0.299 sec  <<< FAILURE!
org.openqa.selenium.UnhandledAlertException: 
unexpected alert open: {Alert text : Click OK to confirm your personal message is correct as this is exactly how it will be printed.}
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 4 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true,
意外警报打开
(会话信息:chrome=58.0.3029.110)
(驱动程序信息:chromedriver=2.29.461591(62ebf098771772160f391d75e589dc567915b233),平台=Windows NT 6.1.7601 SP1 x8664)(警告:服务器未提供任何堆栈跟踪信息)
命令持续时间或超时:63毫秒:null
生成信息:版本:“3.4.0”,版本:“未知”,时间:“未知”
系统信息:主机:'DEV007',ip:'172.16.2.192',os.name:'Windows Server 2008 R2',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0131'
驱动程序信息:org.openqa.selenium.chrome.ChromeDriver
功能[{applicationCacheEnabled=false,rotatable=false,mobileEmulationEnabled=false,networkConnectionEnabled=false,chrome={chromedriverVersion=2.29.461591(62ebf098771772160f391d75e589dc567915b233),userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped\U dir9140\U 30313},takesHeapSnapshot=true,pageLoadStrategy=normal,databaseEnabled=false,handlesAlerts=true,hasTouchScreen=false,version=58.0.3029.110,platform=XP,browserConnectionEnabled=false,nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true,WebStorage=true,browserName=chrome,TakesScreensshot=true,javascriptEnabled=true,cssSelectorsEnabled=true,未预料到的alertbehaviour=}]
会话ID:699ee1257882397cd7695aeaf919c68f
***元素信息:{Using=xpath,value=//按钮[包含(text(),“立即安全支付”)]}
org.openqa.selenium.UnhandledAlertException:
意外警报打开
(会话信息:chrome=58.0.3029.110)
(驱动程序信息:chromedriver=2.29.461591(62ebf098771772160f391d75e589dc567915b233),平台=Windows NT 6.1.7601 SP1 x8664)(警告:服务器未提供任何堆栈跟踪信息)
命令持续时间或超时:63毫秒:null
生成信息:版本:“3.4.0”,版本:“未知”,时间:“未知”
系统信息:主机:'DEV007',ip:'172.16.2.192',os.name:'Windows Server 2008 R2',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0131'
驱动程序信息:org.openqa.selenium.chrome.ChromeDriver
功能[{applicationCacheEnabled=false,rotatable=false,mobileEmulationEnabled=false,networkConnectionEnabled=false,chrome={chromedriverVersion=2.29.461591(62ebf098771772160f391d75e589dc567915b233),userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped\U dir9140\U 30313},takesHeapSnapshot=true,pageLoadStrategy=normal,databaseEnabled=false,handlesAlerts=true,hasTouchScreen=false,version=58.0.3029.110,platform=XP,browserConnectionEnabled=false,nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true,WebStorage=true,browserName=chrome,TakesScreensshot=true,javascriptEnabled=true,cssSelectorsEnabled=true,未预料到的alertbehaviour=}]
会话ID:699ee1257882397cd7695aeaf919c68f
***元素信息:{Using=xpath,value=//按钮[包含(text(),“立即安全支付”)]}
在BuyAGiftFramework.complete.AddingDrivingExperienceToBaskets.clickOn_Pay SecureLynow按钮(AddingDrivingExperienceToBaskets.java:51)
考试结束时间:03.10.30
0
0

测试运行:314次,失败:80次,错误:0次,跳过:214次,经过的时间:62.972秒在这一行中,您已经收到警报:
alert alert=tempWait.until(ExpectedConditions.alertIsPresent())

而不是编写
driver.switchTo().alert().accept(),尝试这样做:
alert.accept()

更新:
如果您只是想不惜一切代价接受警报,最好将try块移到单击线之外。我有充分的理由相信,单击行导致您的未处理警报异常,因为我以前也遇到过同样的问题

尝试更新您的代码,如下所示:

public void clickPaySecurelyNowButton() throws InterruptedException {

    try {
        waitAndclickElementUsingJS(button_PaySecurelyNowTop);

        WebDriverWait tempWait = new WebDriverWait(this.driver, 30);
        tempWait.until(ExpectedConditions.alertIsPresent());
        boolean boolAlert = false;
        int attempts = 0;
        while (!boolAlert && attempts < 1000) {
            attempts++;
            driver.switchTo().alert().accept();
            System.out.println("Successfully clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
            boolAlert = true;
        }
    } catch (org.openqa.selenium.UnhandledAlertException e) {
        driver.switchTo().alert().accept();
        boolAlert = true;
        System.out.println("Sucesffuly clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
    } catch (Exception e) {
        System.out.println("Unable to click on the 'Pay Securely Now Button', Exception: " + e.getMessage());
        Assert.fail("Method failed: clickPaySecurelyNowButton");
    }
}

哪一行抛出erorr?@kushal.im不确定,我已经从jenkins添加了异常行。收到此错误后,您是否有打开的警报?我恐怕已经尝试过此操作,但问题仍然存在,而不是直接从jenkins运行,您是否可以尝试从IDE进行调试,并查看哪一行导致抛出错误?您需要检查是否正在到达
try
块,或者错误是否在
try
块本身之前发生。我以前也遇到过类似的问题,我猜您的错误发生在这一行:
wait和clickelement usingjs(button\u PaySecurelyNowTop)请检查,如果有,我会相应地更新我的答案。问题是很难恢复
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

WebDriver driver = new ChromeDriver(capabilities);