Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- STF
- ftp
- nmap
- postgres
- port forwarding
- nGrinder
- ssh
- Jupyter
- 28015
- kitura
- insert
- centos
- GoCD
- openpyxl
- postgresql
- SWIFT
- sshpass
- rethinkdb
- Materials
- STF_PortForwarding
- ubuntu
- 실행권한
- appium server
- appium
- mysql
- create table
- perfect
- PYTHON
- Jupyter Notebook
- nohup
Archives
- Today
- Total
don't stop believing
Selenium으로 Alert 창 처리하기 본문
Alert 창 처리에 대해 확인해 보겠습니다.
우선 아래와 같이 html 파일을 만들었습니다.
12345678910111213141516171819202122232425262728293031323334353637383940414243<!DOCTYPE html><html><script>function simpleAlert() {alert("Hello! I am a simple alert box!");}function confirmAlert() {var txt;var r = confirm("Press a button!\nEither OK or Cancel.\nThe button you pressed will be displayed in the result window.");if (r == true) {txt = "You pressed OK!";} else {txt = "You pressed Cancel!";}document.getElementById("confirmMemo").innerHTML = txt;}function promptAlert() {var person = prompt("Please enter your name", "Harry Potter");if (person != null) {document.getElementById("inputMemo").innerHTML = "Hello " + person + "! How are you today?";}}</script><body><p>Click the button to display a simple alert box.</p><button onclick="simpleAlert()" id="simpleAlert">Try it</button><p>Click the button to demonstrate line-breaks in a confirm box.</p><button onclick="confirmAlert()" id="confirmAlert">Try it</button><p id="confirmMemo"></p><p>Click the button to demonstrate the prompt box.</p><button onclick="promptAlert()" id="promptAlert">Try it</button><p id="inputMemo"></p></body></html>
실행하면 세개의 Try it 버튼이 있습니다.
python selenium으로 위 Alert 창을 처리하는 script 입니다.
WebDriver로 Alert 창을 Control하고 싶다면 switch_to.alert를 사용하면 됩니다
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990# -*- coding: utf-8 -*-import timefrom selenium import webdriver# Chrome WebDriver를 이용해 Chrome을 실행합니다.driver = webdriver.Chrome('./../chromedriver')# pop.html 파일을 실행합니다.driver.get('file:///Users/tongchunkim/Documents/Test_Selenium/alert_pop/pop.html')time.sleep(2)# 첫 번째 alert 버튼을 클릭합니다.inputElement = driver.find_element_by_id('simpleAlert')inputElement.click()time.sleep(2)# alert 창의 '확인'을 클릭합니다.alert = driver.switch_to.alertalert.accept()time.sleep(2)# 다시 첫 번째 alert 버튼을 클릭합니다.inputElement = driver.find_element_by_id('simpleAlert')inputElement.click()time.sleep(2)# alert 창의 메시지를 확인하고 싶습니다.alert = driver.switch_to.alertmessage = alert.textprint("Alert shows following message: "+ message )time.sleep(2)# 메시지를 확인했으니 창을 닫습니다.alert = driver.switch_to.alertalert.accept()time.sleep(2)# 두 번째 alert 버튼을 클릭합니다.inputElement = driver.find_element_by_id('confirmAlert')inputElement.click()time.sleep(2)# alert 창의 '취소'을 클릭합니다.alert = driver.switch_to.alertalert.dismiss()time.sleep(2)# 다시 두 번째 alert 버튼을 클릭합니다.inputElement = driver.find_element_by_id('confirmAlert')inputElement.click()time.sleep(2)# alert 창의 '확인'을 클릭합니다.alert = driver.switch_to.alertalert.accept()time.sleep(2)# 세 번째 alert 버튼을 클릭합니다.inputElement = driver.find_element_by_id('promptAlert')inputElement.click()time.sleep(2)# alert 창의 '취소'을 클릭합니다.alert = driver.switch_to.alertalert.dismiss()time.sleep(2)# 다시 세 번째 alert 버튼을 클릭합니다.inputElement = driver.find_element_by_id('promptAlert')inputElement.click()time.sleep(2)# prompt창에 이름을 씁니다.alert = driver.switch_to.alertalert.send_keys('tongchun')time.sleep(2)# alert 창의 '확인'을 클릭합니다.alert = driver.switch_to.alertalert.accept()time.sleep(2)# 실행한 브라우저를 닫습니다.time.sleep(5)driver.quit()
참고하시기 바랍니다.
'Testing Automation > Selenium' 카테고리의 다른 글
selenium과 Unittest 같이 사용하기 (0) | 2018.03.28 |
---|---|
Chrome 브라우저 창 크기 조절 과 뒤로가기 앞으로 가기 (0) | 2018.03.22 |
iframe 처리하기 (4) | 2018.03.22 |
element highlight로 확인해 보기 (0) | 2018.03.22 |
Chrome에서 Tab 변경하기 (0) | 2018.03.21 |