如何检查网页的弹出窗口? [英] How to check for webpages' popups?

查看:275
本文介绍了如何检查网页的弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能,如果我编写一个程序在Python中,允许自动浏览给予网站使用机械化检测是否有弹出窗口(建议广告或下载动作...)使用Python?我会很感激任何提示(例如,如果你给我一个图书馆,履行这个任务,我会非常高兴)

解决方案

机械化无法处理JavaScript和弹出窗口:



为了达到这个目标,利用真正的浏览器,无头或不是。这是 selenium 的帮助。它具有弹出对话框的内置支持


Selenium WebDriver内置支持处理弹出对话框
的框。触发后,您可以通过以下操作访问警报:

alert =驱动程序.switch_to_alert()


示例(使用 jsfiddle ):

 来自selenium import webdriver 
$ b $ url url =http://fiddle.jshell.net/ebkXh/show/
driver = webdriver.Firefox()
driver.get(url)

button = driver.find_element_by_xpath('// button [@ type =submit]')

#dismiss
button.click()
driver。 switch_to.alert.dismiss()

#accept
button.click()
driver.switch_to.alert.accept()


另见:


  • 处理弹出式窗口

  • <一个href =https://stackoverflow.com/ques tions / 8631500 / click-the-javascript-popup-through-webdriver>通过webdriver点击JavaScript弹出窗口


Is there a possibility if I code a program in python that allows to automatically browse a given website using mechanize to detect if there are popup windows (suggesting advertisements or downloading actions ...) using Python ?. I would appreciate any hint (for example, if you give me a library that fulfills this task I would be very happy)

解决方案

Mechanize cannot handle javascript and popup windows:

To accomplish the goal, you need to utilize a real browser, headless or not. This is where selenium would help. It has a built-in support for popup dialogs:

Selenium WebDriver has built-in support for handling popup dialog boxes. After you’ve triggerd and action that would open a popup, you can access the alert with the following:

alert = driver.switch_to_alert()

Example (using this jsfiddle):

from selenium import webdriver

url = "http://fiddle.jshell.net/ebkXh/show/"
driver = webdriver.Firefox()
driver.get(url)

button = driver.find_element_by_xpath('//button[@type="submit"]')

# dismiss
button.click()
driver.switch_to.alert.dismiss()

# accept
button.click()
driver.switch_to.alert.accept()

See also:

这篇关于如何检查网页的弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆