项目场景:

学习使用selenium自动操作,练习使用浏览器获取 某音乐 的更多精彩评论时候,需要模拟点击【更多精彩评论】来获得折叠的评论 相关准备工作请参考:- chromedriver驱动的下载、安装

问题描述

模拟点击时候出错。 按照driver.find_element().click()理论上可行,实际会出现错误如下异常:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (340, 6898)

(Session info: chrome=122.0.6261.95)

Stacktrace:

GetHandleVerifier [0x00007FF6BD81AD22+56930]

......

BaseThreadInitThunk [0x00007FFEED387344+20]

RtlUserThreadStart [0x00007FFEED8226B1+33

解决方案:

关键代码:

# 模块导入

from selenium.webdriver.common.action_chains import ActionChains

btn = driver.find_element("class name", "comment__show_all").find_element( "tag ame", "a")

actions = ActionChains(driver)

for i in range(4):

actions.move_to_element(btn)

actions.click()

actions.perform()

附上自己练习的源码

# 导入模块

import time

from bs4 import BeautifulSoup

from selenium import webdriver

from selenium.webdriver.chrome.webdriver import WebDriver

from selenium.webdriver.common.action_chains import ActionChains

def find_text(driver: WebDriver):

'''获取精彩评论的长度'''

page_sorce = driver.page_source

html = BeautifulSoup(page_sorce, "html.parser")

for perTag in html.find_all(class_="mod_hot_comment"):

if perTag.find("h2").text == "精彩评论":

all_comments = perTag.find_all(class_="comment__list_item")

print(len(all_comments))

break

try:

driver = webdriver.Chrome()

# 企鹅** 自己选一首歌,将地址栏地址复制

driver.get("https://.../n/.../songDetail/...")

time.sleep(2)

find_text(driver) # 31

btn = driver.find_element("class name", "comment__show_all").find_element(

"tag name", "a"

)

actions = ActionChains(driver)

for i in range(4):

actions.move_to_element(btn)

actions.click()

actions.perform()

find_text(driver) # 92

finally:

driver.close()

参考

Python的Selenium库使用过程中的常见问题汇总python selenium 异常:- - - ElementClickInterceptedExceptionchromedriver驱动的下载、安装

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: