Python Selenium:瀏覽器彈出框處理
阿新 • • 發佈:2018-12-12
在操作瀏覽器的時候,會經常遇到瀏覽器的警告彈窗。一般的彈窗分為三種:1.警告類彈alert(),顯示警告或其他資訊,用於通知使用者,下方只有一個【確認】按鈕。2.確認類彈窗confirm(),詢問是否繼續某種操作等功能,下方有【確認】和【取消】兩種按鈕。3.訊息類彈窗prompt(),需要輸入一些資訊,比如使用者密碼等,下方會有【確認】和【取消】按鈕。
Alert類
Selenium針對瀏覽器Alert也有一些相應的API來處理,我們先來看下面的介紹。
名稱 | 用法 |
---|---|
accept() | 點選Alert的【確認】按鈕 |
authenticate(username,password) | 給需要驗證的Alert傳送賬號和密碼,預設點選OK |
dismiss() | 點選Alert的【取消】按鈕 |
send_keys(keysToSend) | 在Alert的輸入框輸入資訊 |
text | 獲取Alert上的文言資訊 |
switch_to.alert | 切換到Alert |
對Alert操作有兩種方法,可以使用switch_to.alert,切換到Alert,或者通過Alert(driver)來使用,我們來看下面的實際操作。
例項
首先複製下列的html程式碼,儲存為test.html到與指令碼相同的資料夾下。這個html檔案包含三個按鈕,點選後會彈出三種不同的彈出框,另外還有一個文字區域,顯示剛才的動作。
<!doctype html>
<head>
<title>alert,confirm and prompt</title>
<script type='text/javascript'>
function myFunctionAlert(){
window.alert('this is an alert, it has a confirm button')
document.getElementById('action').value = 'you just clicked confirm button of alert()'
}
function myFunctionConfirm(){
var result = window.confirm('this is a confirm,it has a confirm button and a cancel button')
if(result == true){
document.getElementById('action').value = 'you just clicked confirm button of confirm()'
}else if(result == false){
document.getElementById('action').value = 'you just clicked cancel button of confirm()'
}else{
document.getElementsById('action').value = 'you did nothing'
}
}
function myFunctionPrompt(){
var result = window.prompt('this is a prompt,it has an input and two buttons')
if(result == null){
document.getElementById('action').value = 'you just clicked cancel button of prompt()'
}else if(result == ''){
document.getElementById('action').value = 'you just input nothing and clicked confirm button of prompt()'
}else{
document.getElementById('action').value = 'you just input \'' + result + '\' and clicked confirm button of promt()'
}
}
</script>
<body>
<br>
<button type='button' onclick='myFunctionAlert()'>show alert</button>
<br>
<button type='button' onclick='myFunctionConfirm()'>show confirm</button>
<br>
<button type='button' onclick='myFunctionPrompt()'>show prompt</button>
<br>
<textarea id='action' style="width:200px;height:100px;font-family: Microsoft YaHei"></textarea>
</body>
</head>
首先我們先實現: 1.點選第一個按鈕‘show alert’,然後在彈出的對話方塊中點選【確認】按鈕,並且列印你的動作。 2.點選第二個按鈕‘show confirm’,然後在彈出的對話方塊中點選【取消】按鈕,並且列印你的動作。
# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
import os
driver = webdriver.Chrome()
driver.implicitly_wait(10)
file = 'file:///' + os.path.abspath('test.html')
driver.get(file)
driver.find_element_by_css_selector('body>button:nth-child(2)').click() #使用css選擇器定位,show alert按鈕為body下的第二個子元素
sleep(2)
alert = driver.switch_to.alert #切換到alert
print('alert text : ' + alert.text) #列印alert的文字
alert.accept() #點選alert的【確認】按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #列印剛才的操作(獲取頁面最下方的textarea中文字)
sleep(2)
driver.find_element_by_css_selector('body>button:nth-child(4)').click()
sleep(2)
confirm = driver.switch_to.alert
print('confirm text : ' + confirm.text) #列印confirm的文字
confirm.dismiss() #點選confirm的取消按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value'))
sleep(2)
driver.quit()
接著我們來操作:點選第三個按鈕‘show prompt’,輸入文字後點擊【確認】按鈕。
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.alert import Alert #匯入Alert包
from time import sleep
import os
driver = webdriver.Chrome()
driver.implicitly_wait(10)
file = 'file:///' + os.path.abspath('test.html')
driver.get(file)
driver.find_element_by_css_selector('body>button:nth-child(6)').click()
sleep(2)
prompt = Alert(driver) #例項Alert物件,但使用時前面一定要匯入Alert包
print('prompt text : ' + prompt.text) #列印promt的文言
prompt.send_keys('test prompt') #傳送資訊到輸入框中
sleep(2)
prompt.accept() #點選【確認】按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #列印剛才的操作
sleep(2)
driver.quit()