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 浏览器就会提示这个元素不可点击呢?请大家指点,谢谢!
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.