don't stop believing

Selenium으로 Alert 창 처리하기 본문

Testing Automation/Selenium

Selenium으로 Alert 창 처리하기

Tongchun 2018. 11. 1. 11:03

Alert 창 처리에 대해 확인해 보겠습니다.

우선 아래와 같이 html 파일을 만들었습니다.

<!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를 사용하면 됩니다

# -*- coding: utf-8 -*-
import time
from 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.alert
alert.accept()
time.sleep(2)

# 다시 첫 번째 alert 버튼을 클릭합니다.
inputElement = driver.find_element_by_id('simpleAlert')
inputElement.click()
time.sleep(2)

# alert 창의 메시지를 확인하고 싶습니다.
alert = driver.switch_to.alert
message = alert.text
print("Alert shows following message: "+ message )
time.sleep(2)

# 메시지를 확인했으니 창을 닫습니다.
alert = driver.switch_to.alert
alert.accept()
time.sleep(2)



# 두 번째 alert 버튼을 클릭합니다.
inputElement = driver.find_element_by_id('confirmAlert')
inputElement.click()
time.sleep(2)

# alert 창의 '취소'을 클릭합니다.
alert = driver.switch_to.alert
alert.dismiss()
time.sleep(2)

# 다시 두 번째 alert 버튼을 클릭합니다.
inputElement = driver.find_element_by_id('confirmAlert')
inputElement.click()
time.sleep(2)

# alert 창의 '확인'을 클릭합니다.
alert = driver.switch_to.alert
alert.accept()
time.sleep(2)



# 세 번째 alert 버튼을 클릭합니다.
inputElement = driver.find_element_by_id('promptAlert')
inputElement.click()
time.sleep(2)

# alert 창의 '취소'을 클릭합니다.
alert = driver.switch_to.alert
alert.dismiss()
time.sleep(2)

# 다시 세 번째 alert 버튼을 클릭합니다.
inputElement = driver.find_element_by_id('promptAlert')
inputElement.click()
time.sleep(2)

# prompt창에 이름을 씁니다.
alert = driver.switch_to.alert
alert.send_keys('tongchun')
time.sleep(2)

# alert 창의 '확인'을 클릭합니다.
alert = driver.switch_to.alert
alert.accept()
time.sleep(2)


# 실행한 브라우저를 닫습니다.
time.sleep(5)
driver.quit()

참고하시기 바랍니다.

Comments