欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python selenium 彈出框處理的實(shí)現(xiàn)

 更新時(shí)間:2019年02月26日 11:30:14   作者:雨彡  
這篇文章主要介紹了python selenium 彈出框處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

彈出框有兩種:頁(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)文章

最新評(píng)論