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 浏览器就会提示这个元素不可点击呢?请大家指点,谢谢!
![]() | 1 O14 2017-10-01 10:51:43 +08:00 via Android |
![]() | 2 kerberos 2017-10-02 16:28:46 +08:00 你也得指定 executable_path=‘’ |
![]() | 3 woshichuanqilz 2017-10-02 18:27:07 +08:00 把你的 chrome 源码发下我看看 |
![]() | 4 woshichuanqilz 2017-10-02 18:59:12 +08:00 # -*- 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() 我试了这样的代码也不行 |
5 mlyy 2017-10-04 11:30:34 +08:00 via iPad |
![]() | 6 shn7798 2017-10-07 13:51:22 +08:00 > 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 对网页还原的比较好。。。 不过也有可能窗口太小了,元素都挤在一起了,试试调整一下串口大小。 |
7 saximi OP @shn7798 请问如何调整窗口大小,我用了语句 driver.maximize_window() ,结果报错说窗口已经是最大了。 |
8 saximi OP @woshichuanqilz 不好意思,我贴出的代码中 URL 有错,正确的应该是 http://buy.ccb.com/searchproducts/pv_0_0_0_0_1.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area= 但是这个地址还是无法用 Chrome()来爬,依然说元素无法 click |
9 saximi OP @woshichuanqilz 把 URL 改为正确的 http:// buy.ccb.com/searchproducts/pv_0_0_0_0_1.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area= 这样之后您的代码可以正常运行了,但是我的代码还是提示元素不能 click,请问是怎么回事呢 |
10 saximi OP @mlyy 非常感谢,用帖子中提到的某个方法解决了。问题的原因是对于 Chrome 浏览器,当访问的页面大到需要拖动滚动条才能完全浏览时,对于未拖动滚动条时没出现的元素,是不能 click 的。我觉得这应该是 Chrome 对开发者不够友好的地方吧。 |
![]() | 11 woshichuanqilz 2017-10-11 08:08:19 +08:00 @saximi 我的代码里面有拖动这个页面为什么也不行呢? |
12 saximi OP @woshichuanqilz 我试了你的代码是可以执行的,出错的原因是我主贴中给出的 URL 是错误的,你使用这个正确的 URL 再看看? buy 之前的空格要去掉 http:// buy.ccb.com/searchproducts/pv_0_0_0_0_1.jhtml?query=*&selectCatId=12001001&catId=12001001&isBH=false&area= |