메뉴 건너뛰기

목록
profile
조회 수 623 댓글 2 예스잼 1 노잼 0

No Attached Image

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
  • profile
    하야한아이 2021.01.04 11:16
    보통 내부 로직까진 살펴보지도 않고 그냥 응답값만 오면 그렇구나 하고 쓰는애들이 태반인데
    제대로 배우네 아주 바람직한 자세임
  • profile
    스탈린 2021.01.05 04:30
    내부까지는 뜯어보기 귀찮아서 있어도 안보고 넘어가는데 성격이 아주 세심하노

공지 수용소닷컴 이용약관 asuka 2020.05.16
  1. 타입스크립트 간단히 테스트하기좋은곳

    Date2021.11.22 By으ㅇ유ㅏ」 Views91 Votes1
    Read More
  2. No Image

    저 칭찬해주셈

    Date2021.11.09 By고졸빡통아오바 Views65 Votes1
    Read More
  3. 텔레그램 채널 메세지 번역

    Date2021.10.29 By저능아 Views96 Votes1
    Read More
  4. 음악봇

    Date2021.10.07 By바보 Views107 Votes1
    Read More
  5. No Image

    요즘 RN 공부함

    Date2021.09.08 By마루쉐 Views57 Votes1
    Read More
  6. 학생의 코드를 보는 교수.jpg

    Date2021.09.01 By머스크멜론 Views116 Votes1
    Read More
  7. 감잡았음

    Date2021.08.23 By바보 Views75 Votes1
    Read More
  8. 공부중

    Date2021.08.16 By그리드 Views59 Votes1
    Read More
  9. 서버 하나 또 사야지

    Date2021.08.01 By만년필 Views38 Votes1
    Read More
  10. No Image

    [Python] File.readlines / IndexError: list index out of range

    Date2021.07.31 By토깽이 Views81 Votes1
    Read More
  11. 프론트 잡기술 연습용 앱

    Date2021.07.08 By마루쉐 Views78 Votes1
    Read More
  12. 이거 하드인줄알았음

    Date2021.05.18 By코도모 Views114 Votes1
    Read More
  13. No Image

    씨샵하고 파이썬배우니까 왜캐좆같냐

    Date2021.04.01 By샤프 Views63 Votes1
    Read More
  14. No Image

    요새 심심해서 영상 처리 강의 듣는데 재미져

    Date2021.01.26 ByMDR Views58 Votes1
    Read More
  15. No Image

    4시간 만에 버그 고쳤따..

    Date2021.01.18 By마루쉐 Views104 Votes1
    Read More
  16. No Image

    Selenium alert_is_present 작동 원리

    Date2021.01.04 By우지챠 Views623 Votes1
    Read More
  17. No Image

    오늘 코딩 안 했어! 대신 재밌는 얘기

    Date2021.01.02 By마루쉐 Views85 Votes1
    Read More
  18. No Image

    오늘의 코딩 일기 +잡다한 생각

    Date2020.12.06 By고라니 Views57 Votes1
    Read More
  19. No Image

    모델 CNN 학습할 때 적당한 횟수 알려주는 거 있냐??

    Date2020.12.04 ByMDR Views41 Votes1
    Read More
  20. 옛날에 제가 쓰고 뿌듯하다고 생각한 코드 좀 생각해봐씀

    Date2020.11.24 By고라니 Views67 Votes1
    Read More
목록
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 56 Next
/ 56