Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Selenium Learning: alert frame processing

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report