Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Selenium Learning: alert frame processing

Shulou Source: shulou.com Published: 2022-06-03 04:27:45 11月25日 Update

File name: test12.py

# coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport timedriver = webdriver.Chrome () file_path=' http://www.baidu.com'driver.implicitly_wait(10)driver.get(file_path)link = driver.find_element_by_link_text ('setup') ActionChains (driver). Move_to_element (link). Perform () # Open search Settings driver.find_element_by_link_text ("search Settings") .click ( ) time.sleep (5) # Save settings driver.find_element_by_css_selector ('# gxszButton > a.prefpanelgo'). Click () time.sleep (5) # accept the warning box driver.switch_to_alert (). Accept () time.sleep (10) driver.quit ()

In the above code, first open the home page of Baidu, open the search settings, and after clicking to save the settings, the warning box of "your usage preference has been recorded" will pop up.

Get the alarm box on the current page through driver.switch_to_alert (), and use the accept method to receive the alarm box

It is very simple to deal with alert, confirm and prompt generated by JavaScript in Webdriver. Use the switch_to_alert method to locate the alert/confirm/prompt, and then use the text/accept/dismiss/send_keys method to handle the operation.

Text: returns text information in alert/confirm/prompt

Accept (): accept the existing alert box

Dismiss () dissolves the existing alarm box

Send_keys (keysToSend) sends text to the alert box. KeysToSend text legal warning box

Tags: Methods search processing text code information preferences files file names text legal system pages home pages positioning generation learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn Shulou Tech Info Linux Shulou Information