selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“xpath”,“selector”:".//div[contains(@class,“c-goods-item__market-price”)]/text()"}(Session info: chrome=99.0.4844.51)

抓取某购物网站页面内容,以“小皮鞋”字段为例,代码如图

from selenium import webdriver
import time,random
from pymongo import MongoClient
from selenium.common.exceptions import NoSuchElementExceptionclass Wp():def __init__(self):options = webdriver.ChromeOptions()options.add_argument('--disable-blink-features=AutomationControlled')self.browser = webdriver.Chrome(chrome_options=options)def base(self):# 访问网站self.browser.get('https://www.vip.com/')# 获取输入框input = self.browser.find_element_by_css_selector('div.c-search-form>input')time.sleep(2)# 发送参数input.send_keys('小皮鞋')# # 点击查询self.browser.find_element_by_css_selector('div.c-search-form>a').click()def spider(self):# 下拉翻页self.drop_down()time.sleep(0.5)# 定位数据区域li = self.browser.find_elements_by_xpath('//section[@id="J_searchCatList"]/div')for j in li:# 定位数据price = j.find_elements_by_xpath('.//div[contains(@class,"c-goods-item__sale-price")]/text()')oldprice = j.find_element_by_xpath('.//div[contains(@class,"c-goods-item__market-price")]/text()')discount = j.find_element_by_xpath('.//div[contains(@class,"c-goods-item__discount")]/text()')# 构造数据结构items = {'价钱':price.text,"原价": oldprice.text,"折扣":discount.text,}# 调用保存逻辑self.save_mongo(items)# 翻页self.page_next()def save_mongo(self,data):client = MongoClient()  # 建立连接col = client['python']['xx3']if isinstance(data, dict):res = col.insert_one(data)return reselse:return '单条数据必须是这种格式:{"name":"age"},你传入的是%s' % type(data)def page_next(self):try:# 定位翻页next = self.browser.find_element_by_css_selector('a.cat-paging-next>i')if next:next.click()self.spider()else:self.browser.close()except Exception as e:print(e)def drop_down(self):for x in range(1, 10):j = x / 10js = f"document.documentElement.scrollTop = document.documentElement.scrollHeight * {j}"self.browser.execute_script(js)time.sleep(random.randint(600,1000)/1200)
if __name__ == '__main__':f = Wp()f.base()f.spider()

出现报错如图,不知道如何解决

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“xpath”,“selector”:".//div[contains(@class,“c-goods-item__market-price”)]/text()"}
(Session info: chrome=99.0.4844.51)
Stacktrace:
Backtrace:
Ordinal0 [0x00AB9943+2595139]
Ordinal0 [0x00A4C9F1+2148849]
Ordinal0 [0x00944528+1066280]
Ordinal0 [0x00970FD4+1249236]
Ordinal0 [0x009711CB+1249739]
Ordinal0 [0x009674F1+1209585]
Ordinal0 [0x0098BA34+1358388]
Ordinal0 [0x00967474+1209460]
Ordinal0 [0x0098BC04+1358852]
Ordinal0 [0x0099BAF2+1424114]
Ordinal0 [0x0098B806+1357830]
Ordinal0 [0x00966086+1204358]
Ordinal0 [0x00966F96+1208214]
GetHandleVerifier [0x00C5B232+1658114]
GetHandleVerifier [0x00D1312C+2411516]
GetHandleVerifier [0x00B4F261+560433]
GetHandleVerifier [0x00B4E366+556598]
Ordinal0 [0x00A5286B+2173035]
Ordinal0 [0x00A575F8+2192888]
Ordinal0 [0x00A576E5+2193125]
Ordinal0 [0x00A611FC+2232828]
BaseThreadInitThunk [0x7506FA29+25]
RtlGetAppContainerNamedObjectPath [0x771F7A9E+286]
RtlGetAppContainerNamedObjectPath [0x771F7A6E+238]

解决

进行数据定位的修改

内容数据定位同样进行修改更加具体

爬取结果—monogoDB库中保存

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate elemen相关推荐

  1. 已解决 selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element 找不到元素的问题

    场景 使用selenium的find_element_by_xpath()函数报错:selenium.common.exceptions.NoSuchElementException: Message ...

  2. Selenium Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element

    用的佳哥的python基于selenium的学习通健康自动填报 但是今天突然报错了. Error Message Message: no such element: Unable to locate ...

  3. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element

    在输入框输入内容,点击[查询],然后点击页面上的[处理],并进行点击时,报错,报错内容如下. selenium.common.exceptions.StaleElementReferenceExcep ...

  4. 已解决selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted:

    已解决python selenium模块自动化操作浏览器点击元素,抛出异常selenium.common.exceptions.ElementClickInterceptedException: Me ...

  5. 解决selenium用cookies时候报错selenium.common.exceptions.InvalidArgumentException: Message: invalid argument

    报错如下: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'xxxxx ...

  6. 报错:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versio

    报错: selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versio ...

  7. selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to bein PATH

    使用Selenium模拟浏览器访问淘宝首页,出现报警 from selenium import webdriver import timebrowser = webdriver.Chrome() br ...

  8. selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’解决

    selenium.common.exceptions.WebDriverException: Message: 'chromedriver'解决: https://blog.csdn.net/weix ...

  9. 已解决 selenium.common.exceptions.NoSuchWindowException: Message: no such window

    D:\学习\Python工程\venv\Scripts\python.exe "D:\软件\PyCharm Community Edition 2019.1.3\plugins\python ...

最新文章

  1. rest api_REST API
  2. python过滤敏感词
  3. 满有趣的屏幕快捷键;)
  4. Go聊天室的思路:一个拨号 一个监听
  5. 如何确定coordinator
  6. 【推荐】国外优秀Drupal答疑网站
  7. python中的可迭代对象
  8. 20世纪物理学巨人、诺奖得主菲利普安德森逝世,享年96岁
  9. 微信第三方登录测试时报Scope参数错误或没有Scope权限解决方法
  10. c语言数字游戏程序,C语言实现数字游戏
  11. 基于单片机的简易计算器
  12. 大数据平台的SQL查询引擎有哪些—SparkSQL
  13. Java UTC(GMT)时间和Date时间转换
  14. 局域网有几台电脑频繁断网_关于电脑经常掉线的那些事,解决方法
  15. 【评测】NHEK细胞(正常成人表皮角质形成层细胞)
  16. windows symbols
  17. 75.(leaflet之家)leaflet柱状图
  18. 怎样低成本的实现网页在移动端的适配
  19. Android照片墙加强版,使用ViewPager实现画廊效果
  20. CET-6--2018.12--1

热门文章

  1. iOS实现类似微信聊天气泡
  2. java 枚举转换_Java 枚举与字符串的转换
  3. 第一性原理差分电荷密度计算能得到什么数据?
  4. java jdbc视频下载_Java经典教程_JDBC视频教程 下载百度云下载
  5. W3school:CSS基础:CSS边框、CSS边距、CSS高度/宽度、CSS框模型、CSS轮廓
  6. html足球球面插件,响应式流布局插件DyLay-动画效果很赞哦!
  7. maya刷权重时有个叉_maya怎么为蘑菇刷权重?
  8. yii防护csrf攻击
  9. \t\t【名人名言】人性结点
  10. 警惕手机病毒成偷跑流量“陷阱”