首页 > python > 使用Python中的selenium处理网站中的Pop-under广告

使用Python中的selenium处理网站中的Pop-under广告 (Handling Pop-under ads in Website using selenium in Python)

2017-12-20 pythonselenium

问题

我想在一段时间内提取并发布站点中的一些数据,比如1小时。但该网站以随机间隔显示了弹出广告(广告新标签页)。我该如何预防/处理这些广告?该网站受到验证码保护,因此我无法每次关闭和打开该网站。

解决方法

每隔一段时间调用一段代码,直到完成所需的操作。MainWindow在广告开始弹出之前设置到您开始的窗口:

MainWindow = driver.current_window_handle

def CloseNewWindows(driver, MainWindow):
    Windows = driver.window_handles
    for window in Windows:
        driver.switch_to_window(window)
        if MainWindow != driver.current_window_handle:
            driver.close()

问题

I want to extract and post some data in a site in some intervals say 1 hr. But the site is showing pop-under ads (new tab opens for ads) in random intervals. How could i prevent/handle those ads? The site is captcha protected so i cannot close and open the site every time.

解决方法

Call this piece of code in intervals until you have completed what you need. Set MainWindow to the window you start with before ads start popping up:

MainWindow = driver.current_window_handle

def CloseNewWindows(driver, MainWindow):
    Windows = driver.window_handles
    for window in Windows:
        driver.switch_to_window(window)
        if MainWindow != driver.current_window_handle:
            driver.close()
相似信息