首页 > 解决方案 > 元素在点(x,y)处不可交互/不可点击

问题描述

我试图使用 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)```

标签: pythonpython-3.xweb-scrapingselenium-chromedriver

解决方案


尝试通过文本查找元素"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()

推荐阅读