溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中使用selenium處理彈出框

發(fā)布時(shí)間:2021-04-20 17:09:28 來源:億速云 閱讀:242 作者:Leah 欄目:開發(fā)技術(shù)

怎么在python中使用selenium處理彈出框?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

python是什么意思

Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。

一、頁面彈出框

等待彈出框出現(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)的元素可見
ele_id = "TANGRAM__PSP_10__footerULoginBtn"
param = (By.ID,ele_id)
#元素可見時(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)擊頁面元素,觸發(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的文本內(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' ></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')) #打印剛才的操作(獲取頁面最下方的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()

接著我們來操作:點(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()

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI