7. Selenium其它功能
7.1 Action Chains 动作链功能
WebDriver只能模拟针对特定元素的click, send_keys 操作,无法执行鼠标移动、悬浮、按键,选择菜单等操作,需要通过 Action Chains 类来操作
如下面操作,打开主菜单项后,再点击弹出的子菜单项
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
也可以将多个动作排队,最后再执行
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
Selenium Python动作链方法
可以使用动作链执行大量操作,例如单击,右键单击等。以下是动作链中使用的重要方法的列表
Method | Description |
---|---|
click | Clicks an element. |
click_and_hold | Holds down the left mouse button on an element. |
context_click | Performs a context-click (right click) on an element. |
double_click | Double-clicks an element. |
drag_and_drop | Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button. |
drag_and_drop_by_offset | Holds down the left mouse button on the source element, then moves to the target offset and releases the mouse button. |
key_down | Sends a key press only, without releasing it. |
key_up | Releases a modifier key. |
move_by_offset | Moving the mouse to an offset from current mouse position. |
move_to_element | Moving the mouse to the middle of an element. |
move_to_element_with_offset | Move the mouse by an offset of the specified element, Offsets are relative to the top-left corner of the element. |
perform | Performs all stored actions. |
pause | Pause all inputs for the specified duration in seconds |
release | Releasing a held mouse button on an element. |
reset_actions | Clears actions that are already stored locally and on the remote end |
send_keys | Sends keys to current focused element. |
7.2 Alert 告警提示窗口相关操作
Selenium中的告警窗是一个小消息框,出现在屏幕上,为用户提供一些信息或通知。它通知用户一些特定信息或错误,请求执行某些任务的权限,还提供警告消息。
Alert 告警窗口的类型
1) 简单告警窗
主要用于显示一些信息
2)输入型提示窗
此类告警空会询问用户的一些输入,Selenium 网络驱动程序可以使用 sendkeys(“ input…").
3) 确认窗口
提供几个操作选项,供用户选择
编程步骤
1)每当触发警报时,网页上都会出现一个弹出窗口。控件仅保留在父网页中。因此,Selenium Webdriver 的第一个任务是将焦点从父页面切换到警报弹出窗口。可以使用以下代码片段完成此操作。
alert_obj = driver.switch_to.alert
- 控件移动到弹出窗口后,我们可以使用推荐的方法对其执行不同的操作。
alert_obj.accept() – 用于接受提示窗口
alert_obj.dismiss() – 用于取消提示窗口
alert.send_keys() – 向提示窗口文本输入框输入内容
alert.text() – 获取提示信息
处理简单告警窗口
简单告警窗上有一条消息和一个“确定”按钮。当它弹出时,用户单击“确定”按钮接受它。
这是HTML代码,它将在单击主页上的“创建告警”按钮时生成简单告警
Simple_Alert.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>
下面是selenium处理代码
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 ert
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
处理输入提示框
如下提示框:
selenium代码如下:
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