【问题标题】:Element not interactable/not Clickable at point(x,y)元素在点(x,y)处不可交互/不可点击
【发布时间】:2019-05-27 10:30:56
【问题描述】:

我尝试使用 Selenium 和 chromedriver 从 https://edm.com/news 的多个页面中抓取文章,并在尝试点击“查看更多”按钮时遇到多个错误。关于我可以尝试什么的任何想法?

我尝试过使用 ActionChains.move_to_element(..).click.perform() 还尝试了多次 time.sleep 调用或 WebDriverWait.until... 似乎没有任何效果。

start_url = "https://edm.com/news"
browser = webdriver.Chrome(executable_path='chromedriver.exe',      options=self.option)
browser.get(self.start_url)
# Wait max 10 secs for page to load
timeout = 10
WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="lyra-wrapper"]/div/div[3]/section/'
                                     'div[2]/section[2]/section/div/button')))
time.sleep(2)
button = browser.find_elements(By.XPATH, '//*[@id="lyra-wrapper"]/div/div[3]/section/'
                                         'div[2]/section[2]/section/div/button')[0]
button.click()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (508, 4270)
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17763 x86_64)```

【问题讨论】:

    标签: python python-3.x web-scraping selenium-chromedriver


    【解决方案1】:

    尝试通过文本"See More"查找元素

    start_url = "https://edm.com/news"
    browser = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
    browser.get(start_url)
    
    # Wait max 10 secs for page to load
    wait = WebDriverWait(browser, 10)
    time.sleep(2)                    
    
    close_advert = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lyra-wrapper"]/div/div[4]/phoenix-ad[2]/div/div[2]')))
    close_advert.click()
    
    see_more = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(.,"See More")]'))) 
    
    time.sleep(2)
    
    try:
        see_more.click()
    except:
        time.sleep(3)
        see_more.click()
    

    【讨论】:

    • 嘿@chitown88 首先,我非常感谢您的快速回复,所以提前感谢您的回复。不幸的是,在进行了您推荐的更改后,我仍然运行在相同的“元素在...处不可点击”错误。
    • 奇怪。它对我有用。可能需要在那里延迟时间。我会在上面做广告
    • 哦,你知道吗。有一个弹出窗口阻止它,这就是为什么它说没有可点击的元素。当它出现时,你必须让 selenium 关闭出现的广告
    • 我们取得了一些进展!现在我设法击中了几次然后又出现了另一个错误(其他元素会获得点击,元素在 (x,y) 处不可用)但我想我可以稍微延迟一下来解决这个问题。不敢相信这是弹出的饼干....感谢您的帮助我的朋友!
    • 是的。出于某种原因,它直到向下滚动才识别它。所以这是一种“黑客”解决方案,但它现在对我有用。
    【解决方案2】:
    <button class="m-component-footer--loader m-button" onclick="return phoenixTrackClickEvent(this, event);" phx-track-id="load more">
        <div class="m-component-footer--text m-component-footer--loading">Loading…</div>
        <div class="m-component-footer--text m-component-footer--button-text">See More</div>
    </button>
    

    如果您仔细观察,按钮标签中有 2 个 div。所以你需要使用 xpath 来查看更多 div,即//*[@id="lyra-wrapper"]/div/div[3]/section/div[2]/section[2]/section/div/button/div[2]。然后使用 click() 就可以了。

    【讨论】:

    • 嗨,Saurabh,感谢您的快速回复,不胜感激!问题是它检测到 See More div/button 并且仍然以在 (x,y) 处不可点击的错误告终。真的无法理解为什么会这样。
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2016-10-19
    • 2016-08-22
    • 2021-07-08
    • 2017-11-27
    相关资源
    最近更新 更多