ABOUT ME

Today
Yesterday
Total
  • Selenium에서 요소를 클릭하십시오 (ElementClickInterceptedException).
    카테고리 없음 2020. 8. 4. 17:48

    질문

     

    Kaggle에서 정보를 찾기 위해 Selenium을 사용하고 있습니다.그렇게하기 위해 사람들 솔루션에 대한 정보를 긁어 내고 싶습니다.

    이 URL로 이동하면 https://www.kaggle.com/allen-institute-for-ai/CORD-19-research-challenge/tasks?taskId=882 .작업 끝에 사람들 솔루션이 있습니다.

    여기에 이미지 설명 입력

    해당 요소를 클릭하고 싶습니다.페이지 소스에서 솔루션을 누르면 솔루션의 노트북으로 이동 해야하는 것처럼 보이지 않습니다 (

    요소의 html 은 다음과 같습니다. 여기에 이미지 설명 입력

     

    <li class="sc-qQkIG hIKhtZ">
       <a href="/moghazy" target="_blank" rel="noopener" class="sc-pZMVu kiTgMN">
          <div class="sc-oTPjJ cCLmQH"><img src="https://storage.googleapis.com/kaggle-avatars/thumbnails/777390-kg.jpg" alt="Moghazy" class="sc-pckkE ljVeKr"><img src="/static/images/avatier/avatier-expert@2x.png" alt="expert" class="sc-pjIPr cvnVIi"></div>
       </a>
       <div class="sc-qYgLf hNZirj">
          <div class="sc-pRStN jTbOhh">
             <div class="sc-AxheI sc-fznWqX sc-qanuI frNLaF">COVID-19 Literature Ranking + Web Scraping</div>
          </div>
          <div class="sc-psdQm hrPHBT"><span class="sc-fzpkJw sc-fznzOf sc-oToFz dtnpxb"><a href="/moghazy" target="_blank" rel="noopener" class="sc-fzqMAW sc-fzoydu sc-pbJGu jFfyPB">Moghazy</a> · <span title="Sat May 16 2020 02:44:32 GMT+0300 (Israel Daylight Time)">6 days ago</span> · Notebook · Python · 4 Comments</span></div>
       </div>
       <div class="sc-pJsLC cBTgaO">
          <div class="sc-pZzGt eDxwsZ sc-pkjoF iehzxN">
             <button class="MuiButtonBase-root MuiButton-root sc-oTcWe sc-paYYD hlGLPZ MuiButton-outlined" tabindex="0" type="button" state="no-vote" data-testid="vote-button-icon">
                <span class="MuiButton-label label">
                   <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="angle-up" class="svg-inline--fa fa-angle-up fa-w-10 " role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
                      <path fill="currentColor" d="M177 159.7l136 136c9.4 9.4 9.4 24.6 0 33.9l-22.6 22.6c-9.4 9.4-24.6 9.4-33.9 0L160 255.9l-96.4 96.4c-9.4 9.4-24.6 9.4-33.9 0L7 329.7c-9.4-9.4-9.4-24.6 0-33.9l136-136c9.4-9.5 24.6-9.5 34-.1z"></path>
                   </svg>
                </span>
                <span class="MuiTouchRipple-root"></span>
             </button>
             <button class="MuiButtonBase-root MuiButton-root sc-oTcWe sc-pjUyM kZNtvl MuiButton-outlined Mui-disabled Mui-disabled" tabindex="-1" type="button" disabled="" state="no-vote"><span class="MuiButton-label label">14</span></button>
          </div>
       </div>
    </li>

     

    이 코드를 사용하여 요소를 찾습니다.

    elements = driver.find_element_by_class_name("sc-oUaSW").find_elements_by_xpath(".//li")
    for element in elements:
        element.click()

    하지만 오류가 발생합니다.

    메시지:

    요소 클릭 차단 : 요소 ...을 클릭 할 수 없습니다

    그러나 서버에서 클릭하면 클릭 할 수 있습니다.

    내가 뭘 잘못하고 있죠?

     

    답변1

     

    해결해야 할 몇 가지 문제가 있습니다.

    • Element is not clickable because it is not in focus. You need to move to it first.
    • The click opens a new tab. This requires its own handling.
    • Moving back to the original tab to click the next item.

    이 snippet은 다음과 같이 작동합니다.

    main_window = driver.current_window_handle #this relates to issues 2, 3
    
    elements = driver.find_element_by_class_name("sc-oUaSW").find_elements_by_xpath(".//li")
    for element in elements:
        ActionChains(driver).move_to_element(element).click().perform()  #this addresses issue 1
        #element.click()
        driver.switch_to.window(driver.window_handles[1])   #this relates to issue 2
        WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='KernelViewerContext_KernelTitle-sc-rdaqnd chqxNN']"))) 
        #time.sleep(3)
        #do whatever you wish with the tab/task here
        driver.close()
        driver.switch_to.window(main_window)    # close tab and switch back to original browser tab (issue 3)

    아래의 import가 필요합니다 :

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.action_chains import ActionChains

     

     

     

     

     

    출처 : https://stackoverflow.com/questions/61953127/click-on-an-element-in-selenium-elementclickinterceptedexception

    댓글

Designed by Tistory.