我正在尝试使用主要使用selenium web驱动程序和Java关闭网页上显示的对话框,但没有任何结果。我的代码:
@When("^I am on second page I click on close button in pop up$")
public void i_am_on_second_page_I_click_on_close_button_in_pop_up() throws Throwable {
driver.findElement(By.xpath("//*[@id=\"ngdialog2\"]/div[2]/promo-banner/div/div/div[2]/div/dialog-body/div[1]/div[2]")).click();
driver.switchTo().activeElement();
我得到:java.lang.NullPointerException
java.lang.NullPointerException
at SeconPage.i_am_on_second_page_I_click_on_close_button_in_pop_up(SeconPage.java:17)
at ✽.When I am on second page I click on close button in pop up(SeconPage.feature:7)
我缺少什么?
<div class="dialog-body">
<div ng-transclude="dialogBodySlot">
<dialog-body>
<div class="promo-popup-top">
<picture><img class="promo-popup-image"
ng-srcset="/etc/designs/ryanair/frontend/resources/ui/illustrations/products/family-popup.png, /etc/designs/ryanair/frontend/resources/ui/illustrations/products/family-popup@2x.png"
srcset="/etc/designs/ryanair/frontend/resources/ui/illustrations/products/family-popup.png ,/etc/designs/ryanair/frontend/resources/ui/illustrations/products/family-popup@2x.png">
</picture>
<div class="icon-115 promo-popup-icon-family"><img
ng-src="/etc/designs/ryanair/frontend/resources/ui/illustrations/products/family-extra-logo.png"
src="/etc/designs/ryanair/frontend/resources/ui/illustrations/products/family-extra-logo.png">
</div>
<div class="promo-popup-close" ng-click="$ctrl.onClose()">
<core-icon class="icon-16 fill-c-light-base" icon-id="glyphs.close">
<div>
<svg tabindex="-1" focusable="false" role="img">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/pl/pl/#glyphs.close"
ng-href="/pl/pl/#glyphs.close"></use>
</svg>
</div>
</core-icon>
</div>
<div class="popup-message-shadow"><!----><p ng-if="::!$ctrl.firstName" class="first-message"
translate="common.components.family_extra_popup.message1"
translate-default="">Witaj!</p><!----><!----><h3
class="second-message" translate="common.components.family_extra_popup.message3"
translate-default=""><span class="yellow-text">Zaoszczędź </span>na kolejnej podróży dzięki
naszym super ofertom <span class="yellow-text">Family Extra</span></h3></div>
</div>
<div class="promo-popup-benefits family">
<ul class="family-benefits-list"><!---->
<li ng-repeat="benefit in ::$ctrl.popupBenefits track by $index" class="family-benefit">
<core-icon class="icon-40 family-benefit-icon"
ng-class="::$ctrl.popupResources.class + '-benefit-icon'" icon-id="baggage">
<div>
<svg tabindex="-1" focusable="false" role="img">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/pl/pl/#baggage"
ng-href="/pl/pl/#baggage"></use>
</svg>
</div>
</core-icon>
<span class="family-benefit-text" translate="common.components.family_extra_popup.benefit1"
translate-default="">50% zniżki na bagażu (15 kg) dzieci</span></li><!---->
<li ng-repeat="benefit in ::$ctrl.popupBenefits track by $index" class="family-benefit">
<core-icon class="icon-40 family-benefit-icon"
ng-class="::$ctrl.popupResources.class + '-benefit-icon'"
icon-id="priority-boarding">
<div>
<svg tabindex="-1" focusable="false" role="img">
<use xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="/pl/pl/#priority-boarding"
ng-href="/pl/pl/#priority-boarding"></use>
</svg>
</div>
</core-icon>
<span class="family-benefit-text" translate="common.components.family_extra_popup.benefit2"
translate-default="">50% zniżki na usługę Priority Boarding dla dzieci</span></li><!---->
<li ng-repeat="benefit in ::$ctrl.popupBenefits track by $index" class="family-benefit">
<core-icon class="icon-40 family-benefit-icon"
ng-class="::$ctrl.popupResources.class + '-benefit-icon'" icon-id="insurance-shield">
<div>
<svg tabindex="-1" focusable="false" role="img">
<use xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="/pl/pl/#insurance-shield" ng-href="/pl/pl/#insurance-shield"></use>
</svg>
</div>
</core-icon>
<span class="family-benefit-text" translate="common.components.family_extra_popup.benefit3"
translate-default="">50% zniżki na ubezpieczenie dla dzieci</span></li><!---->
<li ng-repeat="benefit in ::$ctrl.popupBenefits track by $index" class="family-benefit">
<core-icon class="icon-40 family-benefit-icon"
ng-class="::$ctrl.popupResources.class + '-benefit-icon'" icon-id="seat">
<div>
<svg tabindex="-1" focusable="false" role="img">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/pl/pl/#seat"
ng-href="/pl/pl/#seat"></use>
</svg>
</div>
</core-icon>
<span class="family-benefit-text"
translate="common.components.family_extra_popup.mandatory_seat_benefit"
translate-default="">Darmowe miejsca rezerwowane dla dzieci poniżej 12. roku życia</span>
</li><!----></ul>
<button class="core-btn-primary core-btn-phone-full" ng-click="$ctrl.onClose()"
translate="trips.biz_plus_popup.close" translate-default="">Kontynuuj wyszukiwanie
</button>
</div>
</dialog-body>
</div>
</div>
答案 0 :(得分:2)
如果警报肯定在那里:
if(driver.switchTo().alert() != null)
{
Alert alert = driver.switchTo().alert();
alert.dismiss();
}
如果您必须等待它出现:
try {
WebDriverWait wait = new WebDriverWait(driver, 8);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
//handle the exception
}
答案 1 :(得分:0)
尝试按照并告诉我它是否适合您:
@When("^I am on second page I click on close button in pop up$")
public void i_am_on_second_page_I_click_on_close_button_in_pop_up() throws Throwable {
driver.findElement(By.xpath("//*@id='ngdialog2']/descendant::div[@class='dialog-body']/descendant::div[@class='promo-popup-close']")).click();
driver.switchTo().activeElement();
答案 2 :(得分:-1)
((JavascriptExecutor)_driver) .executeScript(“window.onbeforeunload = function(e){};”);