파이썬(Python)/웹스크롤링

파이썬(Python) Selenium Click() 동작이 안될 때

끄적끄적아무거나 2021. 3. 31. 18:48
반응형

 

예약 사이트를 자동화로 만드는 과정에서 몇 몇 버튼이나 a, href 링크들이 클릭이 안되는 경우들이 종종 있었다. 

 

element click intercepted

ElementClickInterceptedException

is not clickable at point

 

위 와 같은 메세지가 발생하거나 다운이되는 현상들이 있었다.

 

내가 예약사이트 자동화중에 만난 경우들을 아래와 같이 정리하고 해결 했던 방법도 정리해 보았다.

 

 

Case 1 


 

그림1

그림1과 같이 화면의 아래 김해CC를 클릭하고 날짜를 클릭 후에 예약하기 버튼을 눌러야 하여서 아래와 같이 코드를 짰다.

kimhae_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/section[1]/div[2]/ul/li[2]/a')))           
kimhae_check = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[2]/ul/li[2]/a')[0]
kimhae_check.click()  

kimhae_date_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/section[1]/div[2]/div[2]/div/ul/li[14]')))           
kimhae_date = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[2]/div[2]/div/ul/li[14]')[0]
kimhae_date.click()         

kimhae_button_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/section[1]/div[2]/button')))           
kimhae_button = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[2]/button')[0]
kimhae_button.click() 

 

presence_of_element_located 를 사용해서 해당 element 가 upload 될 때까지 기다린다음에 클릭을 하였지만 "ElementClickInterceptedException" 에러가 발생하였다. 

 

해결>>

popup_check = WebDriverWait(self.driver, 2).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[1]/div/section[1]/div[3]/button')))           
popup_button = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[3]/button')[0]
popup_button.click()

kimhae_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[1]/div/section[1]/div[2]/ul/li[2]/a')))           
kimhae_check = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[2]/ul/li[2]/a')[0]
kimhae_check.click()  

kimhae_date_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[1]/div/section[1]/div[2]/div[2]/div/ul/li[14]')))           
kimhae_date = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[2]/div[2]/div/ul/li[14]')[0]
kimhae_date.click()         

kimhae_button_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[1]/div/section[1]/div[2]/button')))           
kimhae_button = self.driver.find_elements_by_xpath('/html/body/div[1]/div/section[1]/div[2]/button')[0]
self.driver.execute_script("arguments[0].click();", kimhae_button)

1~4번째 줄처럼 코드를 사용해서 팝업창을 제거 하였다. 원래 사이트에서 팝업창이 몇초 후에 자동으로 없어지기는 하였으나 selenium이 빠르게 동작하기 때문에 팝업창 때문에 클릭이 안되는 현상이 있었다.

 

그리고 마지막 19번째 줄처럼 click을 javascript를 사용해서 클릭하였다. 이유는 selenium으로 click() 함수를 사용하였을 때 실행이 안되는 경우가 종종 있는데 위 execute_script를 사용하니 그러한 문제가 없어 졌다.

 

 

Case 2 


 

그림2

그림2와 같이 선택 버튼을 누르려고 하는데 클리이 안되었다. 

 

해결>>

그림3

그림3은 예약사이트의 날짜와 시간이 클릭되어 있을 경우 선택버튼의 class 가 그림처럼 active 항목이 들어 간다. 만일 클릭이 안되어 있으면 active 부분이 안나온다. 

여기서 발생한 문제는 selenium에서 자동으로 날짜와 시간을 클릭하고 선택버튼을 자동으로 누르려고 하는데 선택버튼의 class는 아직 active 값을 받지 못한 상태였다. 그래서 클릭이 안되고 에러가 발생한 것이다.

 

reserv_btn_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[1]/div/div/div[2]/div[1]/div/button')))           
reserv_btn_class = self.driver.find_elements_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/div/button')[0].get_attribute('class')
print(reserv_btn_class) 

while reserv_btn_class == "select-menu":
reserv_btn_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[1]/div/div/div[2]/div[1]/div/button')))           
reserv_btn_class = self.driver.find_elements_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/div/button')[0].get_attribute('class')
print(reserv_btn_class)
reserv_btn = self.driver.find_elements_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/div/button')[0]            
self.driver.execute_script("arguments[0].click();", reserv_btn)

위 처럼 while을 사용해서 get_attribute('class') 를 통해 읽은 class 가 select-menu 에서 변경이 있을 때 click을 시도하게 변경 하니 정상적으로 클릭을 하였다.

 

 

 

반응형