Python中使用selenium+Chrome进行网页爬虫的问题请教

from selenium import webdriver 
from selenium.webdriver.remote.webelement import WebElement   

url=“http://buy.ccb.com/searchproducts/pv_0_0_0_0_000.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area=

driver =webdriver.PhantomJS(executable_path=r’D:\phantomjs\bin\phantomjs.exe’) #语句 1

driver.get(url)

pg=driver.find_element_by_xpath(’//div[@class=“main”]/div[@class=“right”]/div[@class=“page”]/a[@click][contains(text(),“下一页”)]’).click()

上面的代码用于对 url 下方页码控件中的“下一页”按钮进行点击,以页码为 1 的网页为例,下一页按钮对应的元素是 <div class=“main”>下面的<div class=“right”>下面的<div class=“page”>下面的<a onclick=“queryListByPage(‘2’)”>下一页</a> 代码使用 PhantomJS 浏览器可以正常运行。 但是如果改用 Chrome 浏览器,即把语句 1 改写为 driver =webdriver.Chrome() 后就报出现如下错误:

Traceback (most recent call last): File “d:\Python3\t1.py”, line 37, in <module> pg=driver.find_element_by_xpath(’//div[@class=“main”]/div[@class=“right”]/div[@class=“page”]/a[@onclick][contains(text(),“下一页”)]’).click() File “D:\Python3\lib\site-packages\selenium\webdriver\remote\webelement.py”, line 78, in click self._execute(Command.CLICK_ELEMENT) File “D:\Python3\lib\site-packages\selenium\webdriver\remote\webelement.py”, line 499, in _execute return self._parent.execute(command, params) File “D:\Python3\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 297, in execute self.error_handler.check_response(response) File “D:\Python3\lib\site-packages\selenium\webdriver\remote\errorhandler…py”, line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a onclick=“queryListByPage(‘2’)”>…</a> is not clickable at point (1056, 589). Other element would receive the click: <dt>…</dt> (Session info: chrome=60.0.3112.113) (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 x86)

为何改用 Chrome 浏览器就会提示这个元素不可点击呢?请大家指点,谢谢!


Python中使用selenium+Chrome进行网页爬虫的问题请教

13 回复

帖子回复:

嘿,碰到Selenium+Chrome爬虫的问题了?具体是卡在哪一步了?是环境没配好、元素定位不到,还是反爬处理不过去?

如果是环境问题,先确保你的Chrome浏览器和对应版本的ChromeDriver都装好了,并且路径设置正确。这里给个最基础的启动代码示例,你对照看看:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# 指定ChromeDriver路径
service = Service('/path/to/chromedriver')  # 改成你的实际路径
driver = webdriver.Chrome(service=service)

try:
    driver.get('https://www.example.com')
    # 等页面加载一下
    driver.implicitly_wait(10)
    
    # 举个定位元素的例子:找页面第一个<h1>标签
    element = driver.find_element(By.TAG_NAME, 'h1')
    print(f"找到的标题: {element.text}")
    
finally:
    driver.quit()  # 记得关闭浏览器

如果元素定位有问题,检查一下是不是页面有iframe或者动态加载的内容,可能需要加显式等待(WebDriverWait)。如果是反爬,可能需要加user-agent、处理验证码或者用无头模式。

总结:先跑通基础代码,再具体解决定位或反爬问题。

你也得指定 executable_path=‘’

把你的 chrome 源码发下我看看

-- coding: utf-8 --
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
import time

url=“http://buy.ccb.com/searchproducts/pv_0_0_0_0_000.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area=

driver =webdriver.Chrome()

driver.get(url)

driver.maximize_window()
lenOfPage = driver.execute_script(“window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;”)
match=False
while(match==False):
lastCount = lenOfPage
time.sleep(3)
lenOfPage = driver.execute_script(“window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;”)
if lastCount==lenOfPage:
match=True

driver.find_element_by_css_selector(‘body > div.main > div.right > div.page > a:nth-child(8)’).click()


我试了这样的代码也不行

> selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a onclick=“queryListByPage(‘2’)”>…</a> is not clickable at point (1056, 589). Other element would receive the click: <dt>…</dt>

看着错误提示应该是元素层叠并且被遮盖了,可能 chrome 对网页还原的比较好。。。
不过也有可能窗口太小了,元素都挤在一起了,试试调整一下串口大小。

请问如何调整窗口大小,我用了语句 driver.maximize_window() ,结果报错说窗口已经是最大了。

不好意思,我贴出的代码中 URL 有错,正确的应该是 http://buy.ccb.com/searchproducts/pv_0_0_0_0_1.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area=

但是这个地址还是无法用 Chrome()来爬,依然说元素无法 click

把 URL 改为正确的 http:// buy.ccb.com/searchproducts/pv_0_0_0_0_1.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area=
这样之后您的代码可以正常运行了,但是我的代码还是提示元素不能 click,请问是怎么回事呢

非常感谢,用帖子中提到的某个方法解决了。问题的原因是对于 Chrome 浏览器,当访问的页面大到需要拖动滚动条才能完全浏览时,对于未拖动滚动条时没出现的元素,是不能 click 的。我觉得这应该是 Chrome 对开发者不够友好的地方吧。

我的代码里面有拖动这个页面为什么也不行呢?

我试了你的代码是可以执行的,出错的原因是我主贴中给出的 URL 是错误的,你使用这个正确的 URL 再看看? buy 之前的空格要去掉
http:// buy.ccb.com/searchproducts/pv_0_0_0_0_1.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area=

回到顶部