如何单击关闭或确认按钮或单击背景以关闭弹出窗口

时间:2019-05-01 12:48:48

标签: selenium selenium-webdriver

将商品添加到购物车后会弹出一个窗口,但有时不会弹出 我怎么有循环,如果有弹出然后单击关闭或继续按钮或只是单击背景以将其关闭。 如果没有弹出,请继续下一步。 下面是元素,它很长。请帮助检查

<div class="a-popover-wrapper"><header class="a-popover-header"><h4 class="a-popover-header-content" id="a-popover-header-6">Add to Order</h4><button data-action="a-popover-close" class=" a-button-close a-declarative" aria-label="Close"><i class="a-icon a-icon-close"></i></button></header><div class="a-popover-inner" id="a-popover-content-6" style="height: auto; overflow-y: auto;">
    <div id="abb-interstitial" class="a-section abb-interstitial">
 
                            <div id="abb-intl-product-view-B07KTYTTG9" class="a-section abb-intl-product-view" style="display: block;">

 <!-- only true in interstitial popovers -->

<div class="a-row">
    <div class="a-column a-span4 a-text-center abb-product-view-image-container">
        <img src="https://images-na.ssl-images-amazon.com/images/I/31STSohmOiL._SL160_.jpg" class="a-image-wrapper a-manually-loaded abb-product-image" id="" alt="" data-a-image-source="https://images-na.ssl-images-amazon.com/images/I/31STSohmOiL._SL160_.jpg" data-a-hires="https://images-na.ssl-images-amazon.com/images/I/31STSohmOiL._SL500_.jpg">
    </div>
    <div class="a-column a-span8 a-span-last">
        <h3 class="a-spacing-none">
            <a class="a-link-normal abb-product-title" target="_blank" rel="noopener" href="/dp/B07KTYTTG9/ref=ods_dabb_B07KTYTTG9_productInfo">
                AppleCare+ for 13-inch MacBook Pro (3 Years)
            </a>
        </h3>
        <div class="a-section a-spacing-micro abb-product-merchant-container">
            <span class="a-color-secondary">
                from
                <span class="abb-product-merchant">Amazon  Digital  Services LLC</span>
            </span>
        </div>
        <div class="a-section a-spacing-micro abb-product-reviews">
  
<style type="text/css">
    /* 
    * Fix for UDP-1061. Average customer reviews has a small extra line on hover 
    * https://omni-grok.amazon.com/xref/src/appgroup/websiteTemplates/retail/SoftlinesDetailPageAssets/udp-intl-lock/src/legacy.css?indexName=WebsiteTemplates#40
    */
    .noUnderline a:hover { 
        text-decoration: none; 
    }
</style>


        <div id="averageCustomerReviews" class="a-spacing-none" data-asin="B07KTYTTG9" data-ref="dpx_acr_pop_">
            
            
            
                
                
                    



        <span class="a-declarative" data-action="acrStarsLink-click-metrics" data-acrstarslink-click-metrics="{}">
            





    <span id="acrPopover" class="reviewCountTextLinkedHistogram noUnderline" title="4.0 out of 5 stars">
        <span class="a-declarative" data-action="a-popover" data-a-popover="{&quot;max-width&quot;:&quot;700&quot;,&quot;closeButton&quot;:&quot;false&quot;,&quot;position&quot;:&quot;triggerBottom&quot;,&quot;url&quot;:&quot;/gp/customer-reviews/widgets/average-customer-review/popover/ref=dpx_acr_pop_?contextId=dpx&amp;asin=B07KTYTTG9&quot;}">
            <a href="javascript:void(0)" class="a-popover-trigger a-declarative">
                

<i class="a-icon a-icon-star a-star-4"><span class="a-icon-alt">4.0 out of 5 stars</span></i>
                
            <i class="a-icon a-icon-popover"></i></a>
        </span>
        <span class="a-letter-space"></span>
    </span>


        </span>
        <span class="a-letter-space"></span>
        
        

        

        

        
        
        
        
        
        
        
            
            
                <span class="a-declarative" data-action="acrLink-click-metrics" data-acrlink-click-metrics="{}">
                    <a id="acrCustomerReviewLink" class="a-link-normal" href="#customerReviews">
                        <span id="acrCustomerReviewText" class="a-size-base">2 customer reviews</span>
                    </a>
                </span>
            
                <script type="text/javascript">
                    P.when('A', 'ready').execute(function(A) {
                        A.declarative('acrLink-click-metrics', 'click', { "allowLinkDefault" : true }, function(event){
                            if(window.ue) {
                                ue.count("acrLinkClickCount", (ue.count("acrLinkClickCount") || 0) + 1);
                            }
                        });
                    });
                </script>
            
            
            
            
        
        
        <script type="text/javascript">
            P.when('A', 'cf').execute(function(A) {
                A.declarative('acrStarsLink-click-metrics', 'click', { "allowLinkDefault" : true },  function(event){
                    if(window.ue) {
                        ue.count("acrStarsLinkWithPopoverClickCount", (ue.count("acrStarsLinkWithPopoverClickCount") || 0) + 1);
                    }
                });
            });
        </script>


                
            
        </div>
    


        </div>
        <div class="a-section a-spacing-base abb-product-price">
            <span class="a-size-medium a-color-secondary abb-product-list-price a-text-strike">
                
            </span>

            <span class="a-size-medium a-color-price abb-product-buying-price">
                $269.00
            </span>
        </div>

        

         
            
            
                
                <div class="a-section abb-product-feature-bullets">
                    <ul>
                        
                            
                                <li>AppleCare product must be purchased with an applicable Apple device. Terms &amp; Conditions will be delivered via email post-enrollment and can be downloaded through your Digital Items Page (Your Account--&gt;Games and Software).</li>
                            
                        
                            
                                <li>AppleCare+ will automatically be enrolled at time of purchase, and Proof of Coverage will be emailed within 3 days after your device ships.</li>
                            
                        
                            
                                <li>AppleCare+ for Mac extends your coverage to three years from your AppleCare+ purchase date and adds up to two incidents of accidental damage coverage</li>
                            
                        
                            
                                <li>Each incident of accidental damage coverage is subject to a service fee of $99 for screen damage or external enclosure damage, or $299 for other damage, plus applicable tax</li>
                            
                        
                            
                                <li>Get 24/7 priority access to Apple experts by chat or phone</li>
                            
                        
                            
                                <li>Onsite repair for desktop computers: Request that a technician come to your work site</li>
                            
                        
                            
                                <li>Mail-in repair for portable computers: Mail in your Mac using a prepaid shipping box provided by Apple</li>
                            
                        
                            
                                <li>Carry-in repair: Take your Mac to an Apple Store or other Apple Authorized Service Provider</li>
                            
                        
                            
                                <li>Hardware coverage for your Mac computer, battery, included accessories such as the power adapter, Apple memory (RAM), AirPort, and Apple USB SuperDrive</li>
                            
                        
                            
                                <li>Software support for using macOS and iCloud, quick how-to questions about Apple-branded apps, such as Photos, iMovie, GarageBand, Pages, Numbers, Keynote, and more, connecting to printers and AirPort networks</li>
                            
                        
                    </ul>
                </div>
            
        



    </div>
</div>



                                












<div class="a-row">
    <div class="a-column a-span8 a-push4">
        <div class="a-section a-spacing-mini a-spacing-top-base abb-intl-product-cta">

            <div class="a-row a-spacing-top-base">
                <div data-asin="B07KTYTTG9" class="a-checkbox abb-intl-product-ato"><label><input type="checkbox" name="" value="" autocomplete="off"><i class="a-icon a-icon-checkbox"></i><span class="a-label a-checkbox-label">
                    <span class="a-text-bold">Add AppleCare+ for 13-inch MacBook Pro (3 Years) to your order</span>
                </span></label></div>
            </div>

        </div>
    </div>
</div>



                            </div>
                        
                    

                

            

                
                
                
                

                

            

                
                
                
                

                

            

                
                
                
                

                

            

                
                
                
                

                

            
        

        
            
            






<div id="abb-intl-pop-cta" class="a-section a-spacing-base">
    <div class="a-row">
        

            

            
                <div class="a-column a-span8 a-push4">

                    <span class="a-button a-button-primary abb-intl-ato abb-intl-continue" id="a-autoid-13"><span class="a-button-inner"><input class="a-button-input" type="submit" aria-labelledby="a-autoid-13-announce"><span class="a-button-text a-text-center" aria-hidden="true" id="a-autoid-13-announce">
                        Continue
                    </span></span></span>

                    <span class="a-button a-button-base abb-intl-decline aok-hidden" id="a-autoid-14"><span class="a-button-inner"><input class="a-button-input" type="submit" aria-labelledby="a-autoid-14-announce"><span class="a-button-text a-text-center" aria-hidden="true" id="a-autoid-14-announce">
                        No thanks
                    </span></span></span>

                </div>
            

        
    </div>
</div>

        

    </div>

</div></div>

4 个答案:

答案 0 :(得分:1)

尝试下面的代码。希望它能起作用。

要点击继续,请尝试

    if (driver.findElements(By.cssSelector("div.a-popover-wrapper")).size()>0){
    WebElement element=driver.findElement(By.Xpath("//span[normalize-space(.)='Continue']/parent::span/input[@class='a-button-input']"));
    Actions action = new Actions(driver);
    action.moveToElement(element).click().build().perform();

}

OR

if (driver.findElements(By.cssSelector("div.a-popover-wrapper")).size()>0){
    WebElement element=driver.findElement(By.Xpath("//span[normalize-space(.)='Continue']/parent::span/input[@class='a-button-input']"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();",element);


}

//要点击不,谢谢,请尝试

  if (driver.findElements(By.cssSelector("div.a-popover-wrapper")).size()>0){
    WebElement element=driver.findElement(By.Xpath("//span[normalize-space(.)='No thanks']/parent::span/input[@class='a-button-input']"));
    Actions action = new Actions(driver);
    action.moveToElement(element).click().build().perform();
}

OR

if (driver.findElements(By.cssSelector("div.a-popover-wrapper")).size()>0){
    WebElement element=driver.findElement(By.Xpath("//span[normalize-space(.)='No thanks']/parent::span/input[@class='a-button-input']"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();",element);
}

//要关闭弹出窗口

   if (driver.findElements(By.cssSelector("div.a-popover-wrapper")).size()>0){
     WebElement element=driver.findElement(By.cssSelector("i.a-icon.a-icon-close"));
     Actions action = new Actions(driver);
    action.moveToElement(element).click().build().perform();
}

OR

if (driver.findElements(By.cssSelector("div.a-popover-wrapper")).size()>0){
     WebElement element=driver.findElement(By.cssSelector("i.a-icon.a-icon-close"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();",element);
}

答案 1 :(得分:0)

我正在使用python:

def escape_pop(driver):
    try:
         if len(str(driver.find_element_by_class_name("a-popover-wrapper"))) > 0: # check if popup is present
            actions = ActionChains(driver)
            actions.send_keys(Keys.ESCAPE).perform()
    except:
      pass

答案 2 :(得分:0)

这是逻辑。

// change the action based on scenario
String action = 'Continue';
if (driver.findElements(By.Xpath("//div[@class='a-popover-wrapper']")).size()>0){
    driver.findElement(By.Xpath("//div[@class='a-popover-wrapper']//span[@class='a-button-text a-text-center' and normalize-space(.)='" + action + "']")).click();
}

答案 3 :(得分:0)

尝试了许多方法之后。 下面正在工作。 我添加一个等待元素 然后再点击一次。

        WebDriverWait wait2 = new WebDriverWait(driver, 10);
        wait2.until(ExpectedConditions.elementToBeClickable(By.id("a-autoid-13")));
        driver.findElement(By.cssSelector("i.a-icon.a-icon-close")).click();