gpt4 book ai didi

java - Webdriver:为什么我仍然得到 'Unexpected Alert Open'?

转载 作者:行者123 更新时间:2023-11-30 10:29:11 30 4
gpt4 key购买 nike

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

但我似乎得到了奇怪的 Jenkins 构建,它将(间歇性地) 由于意外警报打开(下面列出的异常)而失败,有什么想法吗?

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++;
}

异常标记:

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,

最佳答案

在这一行中,您已经收到警报:Alert alert = tempWait.until(ExpectedConditions.alertIsPresent());

与其编写 driver.switchTo().alert().accept();,不如尝试这样做:alert.accept();

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

尝试像这样更新您的代码:

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");
}
}

==或==
您可以在驱动程序实例化期间使用 chrome 功能使用全局设置:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

WebDriver driver = new ChromeDriver(capabilities);

关于java - Webdriver:为什么我仍然得到 'Unexpected Alert Open'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44161326/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com