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 작동 원리
조회 수 623 댓글 2 예스잼 1 노잼 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 | 추천 |
---|---|---|---|---|---|
공지 | 수용소닷컴 이용약관 | asuka | 2020.05.16 | 1343 | 1 |
878 | 파이썬 평균함수 출력 도움 좀 3 | 비어있는머리통 | 2020.12.02 | 57 | 0 |
877 | 모델 CNN 학습할 때 적당한 횟수 알려주는 거 있냐?? | MDR | 2020.12.04 | 41 | 1 |
876 | 이번 년도 개발 목표 1 | MDR | 2020.12.04 | 72 | 2 |
875 | 오늘의 swift 코드 리팩토링 +여담 2 | 고라니 | 2020.12.04 | 70 | 2 |
874 | 오늘의 코딩 일기 +잡다한 생각 | 고라니 | 2020.12.06 | 57 | 1 |
873 | 삭제된 게시글입니다. | 우지챠 | 2020.12.06 | 62 | 0 |
872 | 일단 공부는 계속 할 것 3 | 그라드 | 2020.12.12 | 65 | 0 |
871 | 삭제된 게시글입니다. | 우지챠 | 2020.12.14 | 51 | 0 |
870 | 4k모니터는 15인치가 적당한듯 2 | 늒비임 | 2020.12.15 | 77 | 0 |
869 | 삭제된 게시글입니다. | 우지챠 | 2020.12.16 | 50 | 0 |
868 | 여기 뭐냐ㄷㄷ | ましろ | 2020.12.17 | 87 | 0 |
867 | CNN 딥러닝 후기 | MDR | 2020.12.17 | 71 | 0 |
866 | 삭제된 게시글입니다. | 우지챠 | 2020.12.19 | 101 | 0 |
865 | 삭제된 게시글입니다. | 우지챠 | 2020.12.28 | 65 | 0 |
864 | 근황 + 오늘 생각해본 것 1 | 마루쉐 | 2020.12.30 | 64 | 0 |
863 | 오늘 코딩 안 했어! 대신 재밌는 얘기 3 | 마루쉐 | 2021.01.02 | 85 | 1 |
862 | 삭제된 게시글입니다. | 우지챠 | 2021.01.03 | 66 | 0 |
861 | 오늘자 리팩터링 9 | 마루쉐 | 2021.01.03 | 171 | 5 |
» | Selenium alert_is_present 작동 원리 2 | 우지챠 | 2021.01.04 | 623 | 1 |
859 | 오늘 한 프로젝트: 그래픽 광량 표현 12 | 우지챠 | 2021.01.05 | 245 | 8 |
제대로 배우네 아주 바람직한 자세임