使用Selenium阻止意外的弹出窗口

时间:2018-05-13 17:16:11

标签: java selenium-webdriver

我正在自动化电子商务网站,但我在任何页面上都会收到意外的随机弹出窗口。如果我知道弹出窗口将在给定的网页上触发,我可以处理它。如果在任何页面上随机出现该怎么办?

enter image description here

我知道如何阻止弹出窗口但在这种情况下无法使用它。由于这个弹出窗口,我的脚本失败了。 HTML代码:

<div class="acsClassicInner" role="document">
    <div class="acsLogoSpacer">
        <img src="//gateway.foresee.com/sites/barneys/staging/trigger/sitelogo.gif" class="acsSiteLogo" title="" alt="">
        <img src="https://static.foresee.com/logos/foresee/150_67.png" class="acsNoDisplay" title="ForeSee" alt="ForeSee">
        <div title="ForeSee" alt="ForeSee" class="acsVendorLogoSVG"></div>
    </div>
    <h1 id="fsrHeading">We'd welcome your feedback!</h1>
    <p>Thank you for visiting Barneys.com. You have been selected to participate in a brief customer satisfaction survey to let us know how we can improve your experience.</p>
    <p class="acsNoticeAboutSurvey">The survey is designed to measure your entire experience, please look for it at the <u>conclusion</u> of your visit.</p>
    <p class="acsAttribution">This survey is conducted by an independent company ForeSee, on behalf of the site you are visiting.</p>
    <a href="#" tabindex="2" class="acsInviteButton acsDeclineButton" title="No, thanks" role="button" precog_scanned="true">No, thanks</a>
    <a id="acsFocusFirst" tabindex="3" href="about:blank" target="_blank" class="acsInviteButton acsRightButton acsAcceptButton" title="Yes, I'll give feedback (Opens in a new window)" role="button" precog_scanned="true">Yes, I'll give feedback</a>
</div>

2 个答案:

答案 0 :(得分:1)

查看EventFiringWebDriver和WebDriverEventListener,并查找有问题的弹出窗口&gt;假设弹出窗口发生在按钮点击后,您可以在beforeClickOn中查找它,如果确实发生了,请执行操作,否则不要做任何事情。

抱歉,我没有任何Java EventFiringWebDriver代码片段可以分享

http://seleniumhq.github.io/selenium/docs/api/java/index.html

来自selenium github的代码

from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
class MyListener(AbstractEventListener):
    def before_navigate_to(self, url, driver):
        print("Before navigate to %s" % url)
    def after_navigate_to(self, url, driver):
        print("After navigate to %s" % url)

 driver = Firefox()
 ef_driver = EventFiringWebDriver(driver, MyListener())
 ef_driver.get("http://www.google.co.in/")

答案 1 :(得分:-1)

使用以下伪代码创建帮助方法: -
    1.检查是否弹出。
    2.如果发现弹出,则处理它     如果没有,那就回来吧。

现在,每当屏幕上的内容由于某些操作而发生变化时,都会在您的脚本中使用此功能。