单击Selenium中的元素(ElementClickInterceptedException)

时间:2020-05-22 10:39:43

标签: html python-3.x selenium selenium-webdriver

我正在使用Selenium在Kaggle中查找信息。 为此,我希望在人员解决方案上抓取信息。

当我导航至该URL时:https://www.kaggle.com/allen-institute-for-ai/CORD-19-research-challenge/tasks?taskId=882。 任务结束时会有人员解决方案。

enter image description here

我想点击那些元素。 从页面源看,按解决方案似乎不应该导航到该解决方案的笔记本(< a href="/{usename}打开用户信息选​​项卡,而不是笔记本选项卡)。

这是元素html的样子: enter image description here

  <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 个答案:

答案 0 :(得分:1)

有几个问题要解决:

  1. 该元素不可点击,因为它不是焦点。您需要先移至该位置。
  2. 单击将打开一个新选项卡。这需要自己处理。
  3. 移回原始选项卡以单击下一项。

此代码段应该有效:

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)

这些导入才能使以上功能正常工作:

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