python selenium 彈出框處理的實(shí)現(xiàn)
彈出框有兩種:頁(yè)面彈出框(可定位元素能操作)、Windows彈出框(不能直接定位)
一、頁(yè)面彈出框
等待彈出框出現(xiàn)之后,定位彈出框,操作其中元素
如:
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
#點(diǎn)擊百度登錄按鈕
driver.find_element_by_xpath('//*[@id="u1"]//a[@name="tj_login"]').click()
#等待百度登錄彈出框中 要出現(xiàn)的元素可見(jiàn)
ele_id = "TANGRAM__PSP_10__footerULoginBtn"
param = (By.ID,ele_id)
#元素可見(jiàn)時(shí),再進(jìn)行后續(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() 獲取彈出框里面的內(nèi)容
- send_keys(keysToSend) 輸入字符串
如:
#1:定位alert彈出框
#點(diǎn)擊頁(yè)面元素,觸發(fā)alert彈出框
driver.find_element_by_xpath('//*[@id="alert"]').click()
time.sleep(3)
#等待alert彈出框可見(jiàn)
WebDriverWait(driver,20).until(EC.alert_is_present())
#從html頁(yè)面切換到alert彈框
alert = driver.switch_to.alert
#獲取alert的文本內(nèi)容
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()
實(shí)例
首先復(fù)制下列的html代碼,保存為test.html到與腳本相同的文件夾下。這個(gè)html文件包含三個(gè)按鈕,點(diǎn)擊后會(huì)彈出三種不同的彈出框,另外還有一個(gè)文字區(qū)域,顯示剛才的動(dòng)作。
<!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>
首先我們先實(shí)現(xiàn):
1.點(diǎn)擊第一個(gè)按鈕‘show alert',然后在彈出的對(duì)話框中點(diǎn)擊【確認(rèn)】按鈕,并且打印你的動(dòng)作。
2.點(diǎn)擊第二個(gè)按鈕‘show confirm',然后在彈出的對(duì)話框中點(diǎn)擊【取消】按鈕,并且打印你的動(dòng)作。
# -*- 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下的第二個(gè)子元素
sleep(2)
alert = driver.switch_to.alert #切換到alert
print('alert text : ' + alert.text) #打印alert的文本
alert.accept() #點(diǎn)擊alert的【確認(rèn)】按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #打印剛才的操作(獲取頁(yè)面最下方的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() #點(diǎn)擊confirm的取消按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value'))
sleep(2)
driver.quit()
接著我們來(lái)操作:點(diǎn)擊第三個(gè)按鈕‘show prompt',輸入文字后點(diǎn)擊【確認(rèn)】按鈕。
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.alert import Alert #導(dǎo)入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) #實(shí)例Alert對(duì)象,但使用時(shí)前面一定要導(dǎo)入Alert包
print('prompt text : ' + prompt.text) #打印promt的文言
prompt.send_keys('test prompt') #發(fā)送信息到輸入框中
sleep(2)
prompt.accept() #點(diǎn)擊【確認(rèn)】按鈕
print('what you have done is : ' + driver.find_element_by_id('action').get_attribute('value')) #打印剛才的操作
sleep(2)
driver.quit()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決django 向mysql中寫入中文字符出錯(cuò)的問(wèn)題
這篇文章主要介紹了解決django 向mysql中寫入中文字符出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
python實(shí)現(xiàn)批量注冊(cè)網(wǎng)站用戶的示例
今天小編就為大家分享一篇python實(shí)現(xiàn)批量注冊(cè)網(wǎng)站用戶的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python數(shù)據(jù)可視化:冪律分布實(shí)例詳解
今天小編就為大家分享一篇Python數(shù)據(jù)可視化:冪律分布實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
分析Python list操作為什么會(huì)錯(cuò)誤
這篇文章主要介紹了分析Python list操作為什么會(huì)錯(cuò)誤,python搞數(shù)據(jù)分析,在很多方面python有著比Matlab更大的優(yōu)勢(shì),下面來(lái)看看文章具體介紹的相關(guān)內(nèi)容吧,需要的朋友可以參考一下2021-11-11
python開(kāi)發(fā)實(shí)例之Python的Twisted框架中Deferred對(duì)象的詳細(xì)用法與實(shí)例
這篇文章主要介紹了python開(kāi)發(fā)實(shí)例之Python的Twisted框架中Deferred對(duì)象的詳細(xì)用法與實(shí)例,需要的朋友可以參考下2020-03-03
python常用操作之使用多個(gè)界定符(分隔符)分割字符串的方法實(shí)例
在使用Python處理字符串的時(shí)候,有時(shí)候會(huì)需要分割字符,下面這篇文章主要給大家介紹了關(guān)于python常用操作之使用多個(gè)界定符(分隔符)分割字符串的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
python實(shí)現(xiàn)微信打飛機(jī)游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信打飛機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
TensorFlow卷積神經(jīng)網(wǎng)絡(luò)AlexNet實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了TensorFlow卷積神經(jīng)網(wǎng)絡(luò)AlexNet實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
詳解在Python中創(chuàng)建條形圖追趕動(dòng)畫
動(dòng)畫是使可視化更具吸引力和用戶吸引力的好方法。它幫助我們以有意義的方式展示數(shù)據(jù)可視化。Matplotlib是一個(gè)非常流行的數(shù)據(jù)可視化庫(kù),通常用于數(shù)據(jù)的圖形表示以及使用內(nèi)置函數(shù)的動(dòng)畫。本文將用Matplotlib繪制條形圖追趕動(dòng)畫,需要的可以參考一下2022-03-03

