python selenium 彈出框處理的實現(xiàn)
彈出框有兩種:頁面彈出框(可定位元素能操作)、Windows彈出框(不能直接定位)
一、頁面彈出框
等待彈出框出現(xiàn)之后,定位彈出框,操作其中元素
如:
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
#點擊百度登錄按鈕
driver.find_element_by_xpath('//*[@id="u1"]//a[@name="tj_login"]').click()
#等待百度登錄彈出框中 要出現(xiàn)的元素可見
ele_id = "TANGRAM__PSP_10__footerULoginBtn"
param = (By.ID,ele_id)
#元素可見時,再進行后續(xù)操作
WebDriverWait(driver,10).until(EC.visibility_of_element_located(param))
driver.find_element_by_id(ele_id).click()
time.sleep(5)
driver.quit()
二、Windows彈出框
使用 driver.switch_to.alert 切換到Windows彈出框
Alert類提供了一系列操作方法:
- accept() 確定
- dismiss() 取消
- text() 獲取彈出框里面的內容
- send_keys(keysToSend) 輸入字符串
如:
#1:定位alert彈出框
#點擊頁面元素,觸發(fā)alert彈出框
driver.find_element_by_xpath('//*[@id="alert"]').click()
time.sleep(3)
#等待alert彈出框可見
WebDriverWait(driver,20).until(EC.alert_is_present())
#從html頁面切換到alert彈框
alert = driver.switch_to.alert
#獲取alert的文本內容
print(alert.text)
#接受--選擇“確定”
alert.accept()
#2:定位confirm彈出框
driver.find_element_by_xpath('//*[@id="confirm"]').click()
time.sleep(3)
WebDriverWait(driver,20).until(EC.alert_is_present())
alert =driver.switch_to.alert
print(alert.text)
# 接受--選擇“取消”
alert.dismiss()
#3:定位prompt彈出框
driver.find_element_by_id("prompt").click()
time.sleep(3)
WebDriverWait(driver,20).until(EC.alert_is_present())
alert =driver.switch_to.alert
alert.send_keys("jaja")
time.sleep(5)
print(alert.text)
# alert.dismiss()
alert.accept()
實例
首先復制下列的html代碼,保存為test.html到與腳本相同的文件夾下。這個html文件包含三個按鈕,點擊后會彈出三種不同的彈出框,另外還有一個文字區(qū)域,顯示剛才的動作。
<!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>
首先我們先實現(xiàn):
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') #發(fā)送信息到輸入框中
sleep(2)
prompt.accept() #點擊【確認】按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #打印剛才的操作
sleep(2)
driver.quit()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python實現(xiàn)批量注冊網(wǎng)站用戶的示例
今天小編就為大家分享一篇python實現(xiàn)批量注冊網(wǎng)站用戶的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
python開發(fā)實例之Python的Twisted框架中Deferred對象的詳細用法與實例
這篇文章主要介紹了python開發(fā)實例之Python的Twisted框架中Deferred對象的詳細用法與實例,需要的朋友可以參考下2020-03-03
python常用操作之使用多個界定符(分隔符)分割字符串的方法實例
在使用Python處理字符串的時候,有時候會需要分割字符,下面這篇文章主要給大家介紹了關于python常用操作之使用多個界定符(分隔符)分割字符串的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01
TensorFlow卷積神經(jīng)網(wǎng)絡AlexNet實現(xiàn)示例詳解
這篇文章主要為大家介紹了TensorFlow卷積神經(jīng)網(wǎng)絡AlexNet實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11

