Python crawler - Selenium (9) alert box (pop) Processing

There are three pop JavaScript Alert (just OK), Confirmation (OK, Cancel button, etc.), Prompt (input box), and pop-up window is not front-end tools through its positioning, this time by switch_to.alertthe method to locate the pop, and a series of operations.

The key method used in this chapter as follows:

  • switch_to.alert: Locate the alert box
  • text: Get a text message alert box
  • accept(): Accept the existing alert box (corresponding acknowledgment)
  • dismiss(): The dissolution of the existing alert box (equivalent to cancellation)
  • send_keys('文本内容'): Send a text to alert box (for input dialog pop)
  • click(): Mouse click event (other mouse events please refer to the Python reptile - Selenium (5) mouse events )
  • move_to_element(): Hover (Please refer to the Python reptile - Selenium (5) mouse events )
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')

# 鼠标悬停至“设置”链接
link = driver.find_element_by_link_text('设置')
ActionChains(driver).move_to_element(link).perform()
time.sleep(2) #睡两秒,看一下效果

# 打开搜索设置
driver.find_element_by_link_text("搜索设置").click()
time.sleep(2) #睡两秒,看一下效果

# 保存设置
driver.find_element_by_class_name("prefpanelgo").click()
time.sleep(2) #睡两秒,看一下效果

# 定位警告框
alert = driver.switch_to.alert
print(alert.text) # 打印警告框内容
#alert.send_keys('输入内容') #此测试网站不是可输入类型的弹窗,先注释掉
alert.accept() #接受现有警告框,相当于确认
#alert.dismiss() #解散现有警告框,相当于取消
time.sleep(2) #睡两秒,看一下效果

driver.quit()
Welcome attention of the same name micro-channel public number: Program ape Miscellany

Program ape Miscellany

Technology | exchange | welfare

Selenium anthology Portal:

title Brief introduction
Python crawler - Selenium (1) easy to install and use Details of installation and simple to use Selenium is dependent on the environment of Windows and Centos7
Python crawler - Selenium (2) and positioning elements common methods WebDriver Details of the positioning element 8 ways and cooperate click and enter, submit, using the method of obtaining information assertion
Python crawler - Selenium (3) common method of controlling the browser Details of using a custom browser window or full-screen size, browser control back, forward, refresh your browser and other methods of
Python reptile - Selenium (4) configuration parameters startup items Details of the configuration parameters Selenium startup items including no interface mode, the browser window size, the browser User-Agent (request header), etc.
Python reptile - Selenium (5) mouse events Details of use of right-click, double click, drag, hover, etc.
Python reptile - Selenium (6) key events Details of operation of the keyboard, includes almost all common keycaps and key combinations
Python crawler - Selenium (7) multi-window switch Selenium is described in detail how to implement the freedom to switch between different windows
Python crawler - Selenium (8) frame / iframe nested form page Details of how to switch from the current positioning of the body frame / iframe embedded in a page form
Python crawler - Selenium (9) alert box (pop) Processing Details of how to locate and deal with many types of warning popups
Python crawler - Selenium (10) treated drop-down box Details on how to locate and deal with flexible drop-down box
Python reptile - Selenium (11) file upload Details of how elegant by send_keys () the specified file upload
Python reptile - Selenium (12) for login Cookies, Cookies automatically log in and add Details of how to obtain and use Cookies Cookies for automatic logon
Python crawler - Selenium (13) element disposed wait Details how elegant set of elements waiting time, to prevent the program from running too fast positioning element failure
Python crawler - Selenium (14) screen shot Details on how to use the screen shot
Python reptile - Selenium (15) closes the browser Close the window detailed describes two differences

Welcome Message Tucao

Published 63 original articles · 87 won praise · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_44110998/article/details/103693898