Getting selenium automated test Alert / Confirm / Prompt pop-up window treatment

A, Alert / Confirm / Prompt described pop-up window feature


Alert pop-up window:

Prompt the user for information only confirmation button, not by positioning of page elements, without closing the window can not do other operations on the page.

Confirm pop-up window:

Confirmation and Cancel buttons, use the pop-up window can not locate the page elements, without closing the window can not do other operations on the page.

Prompt pop-up window:

Input box, confirm and cancel button, the pop-up page elements can not be positioned, without closing the window can not do other operations on the page.

Note: The three built-in browser window window that can not be located to the elements, the elements can be positioned to require the use of WebElement operation.

 

Two, Alert / Confirm / Prompt operator popup

The first step: the need to get the pop-up window, and the two methods Alert (driver)

alert=driver.switch_to.alert

or

from selenium.webdriver.common.alert import Alert
alert=Alert(driver)

Step two: on the acquired operation window, a common method as follows:

alert.text()  # 获取窗口信息 alert.accept() # 确认 alert.dismiss() # 取消 alert.send_keys(keysToSend) # 输入信息

alert.authenticate (username, password) # user login authentication information, the operation has been confirmed

 

Third, the example shows

Create the following three html files

alertTest.html

<html>
<head> <title>Alert Test</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> </head> <script type="text/javascript"> function showAlert(){ alert(document.from1.t1.value); } function showMultilineAlert(){ alert("你必须纠正以下错误:\n你必须输入XXXX.\n你必须做XXXX.\n你必须XXXX"); } </script> <body> <h2>Alert Test</h2> <form name="from1"> <input type="text" name="t1" value="可以输入 Alert 信息"><br><br> <input type="button" name="button1" value="点击Alert获取输入框信息" onclick="showAlert()"><br><br> <input type="button" name="button2" value="Alert自带多行文本信息" onclick="showMultilineAlert()"><br> </form> </body> </html>

 

confirmTest.html

<html>
<head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Confirm Test</title> </head> <script type="text/javascript"> function showConfirm(){ var t1 = document.from1.t1; if (confirm("请点击确认或取消")){ t1.value = "确认"; }else{ t1.value = "取消"; } } </script> <body> <h2>Confirm Test</h2> <form name="from1"> <input type="button" name="button1" value="点击Confirm按钮" onclick="showConfirm()"><br><br> <input type="text" name="t1"> </form> </body> </html>

 

promptTest.html

<html>
<head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Prompt Test</title> </head> <script type="text/javascript"> function showPrompt(){ var t1 = document.from1.t1; t1.value = prompt("请输入信息,信息将填入页面输入框."); } </script> <body> <h2>Prompt Test</h2> <form name="from1"> <input type="button" name="button1" value="点击Prompt按钮" onclick="showPrompt()"><br><br> <input type="text" name="t1"> </form> </body> </html>

 

Example 1: Alert pop gets text confirmation operation

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support.expected_conditions import alert_is_present from selenium.webdriver.common.alert import Alert driver = webdriver.Chrome() driver.get(r'E:\XXX\alertTest.html') driver.find_element_by_name('button1').click() # 点击第一个按钮 WebDriverWait(driver, 5).until(alert_is_present()) # 等待弹出窗口出现 alert = driver.switch_to.alert # 获取弹出窗口 text1 = alert.text # 获取窗口文本信息 print(text1) # 打印窗口文本信息 alert.accept() # 确认 print('----------') driver.find_element_by_name('button2').click() # 点击第二个按钮 WebDriverWait(driver, 5).until(alert_is_present()) # 等待弹出窗口出现 alert = Alert(driver) # 获取弹出窗口 text1 = alert.text # 获取窗口文本信息 print(text1) # 打印窗口文本信息 alert.accept() # 确认 driver.quit()

Note: WebDriverWait (driver, 5) .until (alert_is_present ()) plus the improved reliability of the code

 

Example 2: Comfirm pop acquired text, confirm, cancel

driver = webdriver.Chrome() driver.get(r'E:\XXX\confirmTest.html') driver.find_element_by_name('button1').click() # 点击按钮 WebDriverWait(driver, 5).until(alert_is_present()) # 等待弹出窗口出现 alert = driver.switch_to.alert # 获取弹出窗口 print(alert.text) # 打印窗口信息 alert.accept() # 确认 time.sleep(2) driver.find_element_by_name('button1').click() # 点击按钮 WebDriverWait(driver, 5).until(alert_is_present()) # 等待弹出窗口出现 alert = driver.switch_to.alert # 获取弹出窗口 alert.dismiss() # 取消 time.sleep(2) driver.quit()

 

Example 3: Prompt pop acquired text input, confirmation operation

driver = webdriver.Chrome() driver.get(r'E:\XXX\promptTest.html') driver.find_element_by_name('button1').click() # 点击按钮 WebDriverWait(driver, 5).until(alert_is_present()) # 等待弹出窗口出现 alert = Alert(driver) # Alert 获取弹出窗口 alert.send_keys('selenium Alert弹出窗口输入信息') # 输入信息 alert.accept() # 确认 time.sleep(2) driver.quit()

Guess you like

Origin www.cnblogs.com/jiachangwei/p/12148639.html