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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
셀레늄에서 웹 팝업 알림(alert)가 떠있는지 체크하는 방법이 알고 싶었음
인터넷에 검색해봐도 제가 원하는게 없어서 소스코드를 확인해보기로 함
먼저 웹 팝업 알림이 생길때까지 대기하라는 코드 용법은
WebDriverWait(browser, 10).until(expected_conditions.alert_is_present())
인데
여기서 조건인 alert_is_present()가 팝업 알림 체크를 하지 않을까 생각해서 소스 코드를 봤는데 다음과 같았음
class alert_is_present(object):
""" Expect an alert to be present."""
def __init__(self):
pass
def __call__(self, driver):
try:
alert = driver.switch_to.alert
return alert
except NoAlertPresentException:
return False
조건이 알림이 떠있는지 확인하는 방법은
__call__ method(알아보니 오브젝트에 function처럼 괄호 붙여주면 시행되는거라고 함)에
driver.switch_to.alert를 시행해서 성공하면 alert 오브젝트가, 실패하면 False 값이 나오게 되는거였음
그렇다면 if alert_is_present()(driver) != False:
처럼 사용하면 알림이 떠있을때를 확인할 수 있게 되지 않을까? 라고 생각했지만
그전에 driver.switch_to.alert이 대체 뭔지 알아봐야했음
먼저
from selenium import webdriver
driver = webdriver.Firefox()
를 해주면 firefox webdriver class로부터 오브젝트가 만들어지게 됨
class WebDriver(RemoteWebDriver):
def __init__(self, ...):
...
...
RemoteWebDriver.__init__(self, ...)
이게 firefox webdriver class인데
class init method 안에 parent class인 remotewebdriver class의 init method가 들어있음
이런식으로 init method를 사용해도 되는지 처음 알았음...
remotewebdriver class를 보면
class WebDriver(object):
def __init__(self, ...):
...
...
self._switch_to = SwitchTo(self)
@property
def switch_to(self):
"""
:Returns:
- SwitchTo: an object containing all options to switch focus into
:Usage:
alert = driver.switch_to.alert
"""
return self._switch_to
이렇게 만들어질때 object attribute '._switch_to'에 SwitchTo class 로부터 만들어진 오브젝트를 저장함
그리고 driver.switch_to 를 썼을때 return되는게 이 ._switch_to 오브젝트인걸 알 수 있음
SwitchTo class 를 보면
class SwitchTo:
def __init__(self, driver):
self._driver = driver
@property
def alert(self):
alert = Alert(self._driver)
alert.text
return alert
이런 클래스인데 driver.switch_to.alert 메소드를 쓰게 되면 Alert class로부터 오브젝트를 만들고, alert.text를 시행한다음 리턴하는걸 볼수있음
그래서 Alert class로 가보면
class Alert(object):
def __init__(self, driver):
self.driver = driver
@property
def text(self):
"""
Gets the text of the Alert.
"""
if self.driver.w3c:
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
else:
return self.driver.execute(Command.GET_ALERT_TEXT)["value"]
__init__에서는 아무것도 안하는데
alert.text에서는 alert의 text를 가져오게됨
결론:
driver.switch_to.alert를 하게 되면
이 alert.text가 시행되면서 text를 가져오는 시도를 하게 되고,
만약 text를 가져오는게 실패하면 exception을 throw하는 거였음
이상이 alert_is_present가 팝업알림이 떠있는지 확인하는 방법이었음
소스 코드 해독하느라 대가리가 깨지는줄 알았음
무슨 원리로 작동하는지 알려면 클래스를 몇개를 봐야하는거냐... 이런게 OOP의 폐해인거같음
마지막으로 알림 체크하는 함수를 만들어봄
아직 시행은 귀찮아서 안해봤음
def check_alert(driver)
from selenium.common.exceptions import NoAlertPresentException
try:
driver.switch_to.alert
return True
except NoAlertPresentException:
return False
이제 저는 자러가야겠음
|
cs |
2021.01.04 07:18
Selenium alert_is_present 작동 원리
조회 수 621 댓글 2 예스잼 1 노잼 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 | 추천 |
---|---|---|---|---|---|
공지 | 수용소닷컴 이용약관 | asuka | 2020.05.16 | 1312 | 1 |
1109 | gitlab쓰지 마셈 15 | 다람쥐 | 2020.06.11 | 671 | 0 |
» | Selenium alert_is_present 작동 원리 2 | 우지챠 | 2021.01.04 | 621 | 1 |
1107 | Microsoft Visual Studio 2022를 사용하여 첫 번째 C++ Windows Form 만들기 9 | 저능아 | 2023.01.15 | 379 | 1 |
1106 | 성님들도 서버호스팅 하나 받으셈 14 | 머스크멜론 | 2021.02.24 | 330 | 0 |
1105 | 해피해킹 프로2 type-s 사용기 8 | 다람쥐 | 2020.05.24 | 330 | 2 |
1104 | RAID5 순차쓰기 성능이 너무 낮음... 1 | 아메 | 2021.12.29 | 247 | 0 |
1103 | 정보) 수용서의 기본소양 1편, 짤검색에 대해서 araboji. 8 | 하루각하 | 2021.01.06 | 245 | 6 |
1102 | 오늘 한 프로젝트: 그래픽 광량 표현 12 | 우지챠 | 2021.01.05 | 245 | 8 |
1101 | 좆본 IT 취업 가이드 ~ 간략판 ~ 9 | 抱き枕 | 2020.07.22 | 243 | 6 |
1100 | 삭제된 게시글입니다. | 노모현 | 2020.06.03 | 243 | 0 |
1099 | 키 마우스 매핑 프로그램 만들었음 10 | '`' | 2022.08.30 | 206 | 6 |
1098 | 스프링에서 파일업로드 개발하는데 왤케 에러나냐 8 | 阿米娅 | 2020.08.12 | 197 | 0 |
1097 | 시발 리눅스 SSH서버 공개키로그인이 왜안되나 했는데 5 | 히마와리 | 2020.06.21 | 196 | 0 |
1096 | 콤퓨타 ㅍㅌㅊ? 11 | 문향 | 2020.05.19 | 191 | -1 |
1095 | 대학생 때 세웠던 목표를 이뤘음 7 | 마루쉐 | 2021.09.14 | 188 | 10 |
1094 | 삭제된 게시글입니다. | 스마일 | 2020.06.12 | 182 | 0 |
1093 | 본인 노트북 자랑해봄 4 | ハンター | 2020.05.20 | 181 | 0 |
1092 | 삭제된 게시글입니다. | 노모현 | 2020.05.30 | 173 | 0 |
1091 | 오늘자 리팩터링 9 | 마루쉐 | 2021.01.03 | 171 | 5 |
제대로 배우네 아주 바람직한 자세임