Python:在Selenium中处理警报alert和弹出框Popup,操作alert、confirm、prompt对话框的方法, How to Handle Alert & Pop-up Boxes in Selenium Python

 

在Selenium Python系列教程中,我们将学习处理网页上的警报和弹出框。Web应用程序通常显示警报消息以确认用户操作的标准做法。

警报是一个弹出窗口。由于用户执行了某些操作或由于某些系统设置而自动触发了它。

它们的目的是向用户提供一些信息(也可以是警告),或者征得用户的许可,或者接受用户的一些输入。

 

我们可以将警报大致分为以下三种类型。

i)简单警报 Simple Alert
ii)确认警报 A Confirmation Alert
iii)提示警报 Prompt Alert

现在,我们将详细讨论如何处理以上三种类型的警报。

 

简介–处理警报和弹出框

每当触发警报时,网页上就会出现一个弹出窗口。该控件仅保留在父网页上。因此,Selenium Webdriver的第一个任务是将焦点从父页面切换到Alert弹出窗口。可以使用以下代码段完成。

alert_obj = driver.switch_to.alert

控件移至“警报”弹出窗口后,我们可以使用推荐的方法对它执行不同的操作。

  • alert_obj.accept()–用于接受警报
  • alert_obj.dismiss()–用于取消警报
  • alert.send_keys()–用于在“警报”文本框中输入一个值。
  • alert.text()–用于检索“警报”弹出窗口中包含的消息。

 

处理简单的警报

一个简单的警报上有一条消息和一个“确定”按钮。当它弹出时,用户单击“确定”按钮以接受它。

这是HTML代码,单击主页上的“创建警报”按钮将生成一个简单警报。

<!DOCTYPE html>
<html>
<body bgcolor="#C0C0C0">
<h1>
Simple Alert Demonstration</h1>
<p>
click the Below Button to create an Alert</p>
<button onclick="alertFunction()" name ="alert"> Create Alert</button>
<script>
function alertFunction() {
 alert("Hi!, I am a Simple Alert. Please Click on the 'OK' Button.");
}
</script>
</body>
</html>

 

您需要复制以上HTML代码并将其另存为“ Simple_Alert.HTML”。

Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python
Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python

以下是处理简单警报的代码段。

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.maximize_window()
location = "file://<Specify Path to Simple_Alert.HTML>"
driver.get(location)

#Click on the "Alert" button to generate the Simple Alert
button = driver.find_element_by_name('alert')
button.click()

#Switch the control to the Alert window
obj = driver.switch_to.alert

#Retrieve the message on the Alert window
msg=obj.text
print ("Alert shows following message: "+ msg )

time.sleep(2)

#use the accept() method to accept the alert
obj.accept()

print(" Clicked on the OK Button in the Alert Window")

driver.close

上面的代码模拟了“简单警报”,然后将控件切换到“警报”窗口。之后,它会在“警报”窗口中验证消息并接受。

 

处理确认警报

它类似于简单警报。只是它有一个额外的“取消”按钮以根据需要关闭警报。

以下HTML代码将生成一个确认警报。

<!DOCTYPE html>
<html>
<body bgcolor="#C0C0C0">
<h1>
Confirmation Alert Demonstration</h1>
<p>
click the Below Button to create an Alert</p>
<button onclick="alertFunction()" name ="alert"> Create Alert</button>
<p id ="msg"> 
</p>
<script>
function alertFunction() {
 var txt;
 if (confirm("Hi!! I am Confirmation Alert.")) {
 
 txt = "You pressed OK!"

} else {

txt = "You pressed CANCEL!"
 }
 document.getElementById("msg").innerHTML = txt;
}
</script>
</body>
</html>

 

我已将此文件保存在本地桌面上,名称为“ Confirmation_Alert.HTML”。

Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python
Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python

 

这是处理确认警报的代码。

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.maximize_window()
location = "file://<Specify Path to Confirmation_Alert.HTML>"
driver.get(location)

#Click on the "Alert" button to generate the Confirmation Alert
button = driver.find_element_by_name('alert')
button.click()

#Switch the control to the Alert window
obj = driver.switch_to.alert

#Retrieve the message on the Alert window
message=obj.text
print ("Alert shows following message: "+ message )

time.sleep(2)

#Section 1
#use the accept() method to accept the alert
obj.accept()

#get the text returned when OK Button is clicked.
txt = driver.find_element_by_id('msg')
print(txt.text)

time.sleep(2)

#refresh the webpage
driver.refresh()

# Section 2
# Re-generate the Confirmation Alert
button = driver.find_element_by_name('alert')
button.click()

time.sleep(2)

#Switch the control to the Alert window
obj = driver.switch_to.alert

# Dismiss the Alert using
obj.dismiss()

#get the text returned when Cancel Button is clicked.
txt = driver.find_element_by_id('msg')
print(txt.text)

driver.close

 

现在让我们总结一下上面代码中提到的步骤。

  • 首先,将上述HTML代码保存在计算机中的“ Confirmation_Alert.HTML”名称下。
  • 我们必须在Webdriver代码中定义的占位符中指定上述路径,以处理确认警报。
  • 现在,在代码的第1部分中,我们单击“确定”按钮以接受警报。之后,我们打印了屏幕上显示的返回消息。
  • 在代码的第2节中,我们将再次打开“确认”警报。这次我们单击“取消”按钮以关闭警报框。之后,我们打印了屏幕上显示的返回消息。

 

处理提示警报

它也类似于前两个警报。唯一的区别是,提示允许我们通过它输入一些文本。

以下是将生成提示警报的HTML代码。

<!DOCTYPE html>
<html>
<body bgcolor="#E6E6FA">
<h1>
Employee Login</h1>
<button onclick="myFunction()"name ="employeeLogin">Continue</button>

<p id="msg">
</p>
<script>
function myFunction() {
 var login = prompt("Please re-enter your name", "xxxxxx");
 
 if (login != null) {
 document.getElementById("msg").innerHTML =
 "Welcome " + login + "!! How can I help you?";
 alert("You have logged in successfully !!")
 }
}
</script>
</body>
</html>

 

您需要将此文件另存为“ Prompt_Alert.HTML”。处理此警报也很简单。将焦点转移到警报后,我们可以使用send_keys()方法在其中输入文本。

Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python
Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python

 

这是相同的完整代码。

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.maximize_window()
location = "file://<Specify Path to Prompt_Alert.HTML>"
driver.get(location)

#Click on the "employeeLogin" button to generate the Prompt Alert
button = driver.find_element_by_name('continue')
button.click()

#Switch the control to the Alert window
obj = driver.switch_to.alert

time.sleep(2)

#Enter text into the Alert using send_keys()
obj.send_keys('Meenakshi')

time.sleep(2)

#use the accept() method to accept the alert
obj.accept()

#Retrieve the message on the Alert window
message=obj.text
print ("Alert shows following message: "+ message )

time.sleep(2)

obj.accept()

#get the text returned when OK Button is clicked.
txt = driver.find_element_by_id('msg')
print(txt.text)

driver.close

让我们详细看一下代码。

  • 首先,保存HTML代码以在计算机上创建名称为“ Prompt_Alert.HTML”的提示警报。
  • 您必须在代码中提供的占位符中提供此文件的路径。指定网页的URL时,必须使用正斜杠。否则,它可能无法正常工作。

 

例如,在这里我必须给出文件的路径为。

location = "file://C:\Users/Automation-Dev/Desktop/Meenakshi/Selenium Python/Prompt_Alert.HTML
  • 使用<driver.switch_to.alert>将控件移至警报后, 使用“ send_keys()”方法在警报窗口中输入文本。

 

快速总结–处理警报和弹出框

了解如何使用Selenium Python处理警报和弹出框至关重要。您可以应用以上技术来解决项目中的实时用例。

 

 

本文:Python:在Selenium中处理警报alert和弹出框Popup, How to Handle Alert & Pop-up Boxes in Selenium Python

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.