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

How to automate the testing of python selenium core technology deals with pop-up boxes

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to automate python testing selenium core technology to deal with the pop-up box, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

There are generally three kinds of pop-up boxes on the page:

(1) alert: used to prompt

(2) confirm: used to confirm

(2) prompt: input content

Sample website: http://sahitest.com/demo

Example scenario: open the Sahi Tests page

(1) Click the "Alert Test" page, click the "Click For Alert" button on the page, pop up the warning prompt box to determine whether there is an alert on the page, if so, switch to the warning box and get the warning text information, then click the OK button in the warning box.

(2) Click the "Confirm Page" page, click the "Click For Confirm" button on the page, pop up a confirmation prompt box to determine whether there is an alert on the page, if so, switch to the warning box and get the warning text information, then click the "OK" or "cancel" button in the warning box.

(3) Click the "Prompt Page" page, click the "Click For Prompt" button on the page, pop up a confirmation prompt input box to determine whether there is an alert on the page, if so, switch to the warning box to get the warning text information or enter the text content, and then click the "OK" or "cancel" button in the warning box.

Complete sample script:

From selenium import webdriverfrom time import sleepfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ec class TestWaitCondition (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get (https://sahitest.com/demo/) def test_alert (self): Alert Test self.driver.find_element_by_xpath ("/ html/body/table/) on # dot page Tbody/tr/td [3] / a [1] "). Click () # Mouse click the" Click For Alert "button on the page self.driver.find_element_by_name (" b1 ") .click () wait = WebDriverWait (self.driver 2) # wait and determine whether the warning box exists wait.until (ec.alert_is_present ()) # switch to the alert page alert = self.driver.switch_to.alert # get and print the text in the warning box print (alert.text) # confirm alert.accept () self.driver in the pop-up warning box Click () def test_confirm (self): # Click the Confirm Page link to open the page self.driver.find_element_by_xpath ("/ html/body/table/tbody/tr/td [3] / a [2]"). Click () # Click the Click For Confirm button Pop-up confirmation prompt box self.driver.find_element_by_name ("b1") .click () # switch to warning box confirm=self.driver.switch_to.alert # get warning box prompt message and print print (confirm.text) # Point OK # confirm.accept () sleep (2) # Point cancel Confirm.dismiss () sleep (2) self.driver.quit () def test_prompt (self): # Click the Prompt Page link to open the page self.driver.find_element_by_xpath ("/ html/body/table/tbody/tr/td [3] / a [3]"). Click () # Click the Click For Prompt button Pop-up confirmation prompt box self.driver.find_element_by_name ("b1") .click () sleep (2) # switch to prompt box prompt = self.driver.switch_to.alert # get the text of the prompt box and print print (prompt.text) # enter the text test prompt prompt.send_keys ("test prompt") in the prompt box # confirm prompt.accept () sleep (2) # cancel # prompt.dismiss () self.driver.quit () read the above content Have you mastered how python automates testing and how the core technology of selenium deals with pop-up boxes? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report