V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
? Learn Python the Hard Way
Python Sites
? PyPI - Python Package Index
? http://diveintopython.org/toc/index.html
? Pocoo
值得关注的项目
? PyPy
? Celery
? Jinja2
? Read the Docs
? gevent
? pyenv
? virtualenv
? Stackless Python
? Beautiful Soup
? 结巴中文分词
? Green Unicorn
? Sentry
? Shovel
? Pyflakes
? pytest
Python 编程
? pep8 Checker
Styles
? PEP 8
? Google Python Style Guide
? Code Style from The Hitchhiker's Guide
yezhiye
V2EX  ?  Python

Python selenium find_element_by_xpath 出错

  •  
  •   yezhiye · 2020-02-03 22:20:04 +08:00 · 2664 次点击
    这是一个创建于 1561 天前的主题,其中的信息可能已经有所发展或是发生改变。

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    没事在家写 selenium 爬虫,已知第 2 个可用,第 1 个不可用。查了下 xpath 语法没问题啊……求指教

    WebDriverWait(driver,timeout=15).until(lambda d:d.find_element_by_xpath("//div[@class='article']"))

    WebDriverWait(driver,timeout=15).until(lambda d:d.find_element_by_class_name("article"))

    6 条回复  ?  2020-02-04 08:57:07 +08:00
    yezhiye
        1
    yezhiye  
    OP
       2020-02-03 22:33:48 +08:00 via Android
    class 包含多个属性时 xpath 不能这么写,结帖。
    RedisMasterNode
        2
    RedisMasterNode  
       2020-02-03 22:37:37 +08:00   ?? 1
    xpath 直接从源码中右键,copy xpath 获取比手写靠谱一点?
    yezhiye
        3
    yezhiye  
    OP
       2020-02-03 22:45:23 +08:00 via Android
    @RedisMasterNode copy path 提供的是很笨的那种,页面稍微变动就用不了了
    ClericPy
        4
    ClericPy  
       2020-02-03 23:45:30 +08:00   ?? 1
    1. 没有源码, 不知道你这定位到哪去了
    2. 没有结果, 不知道你说的 find 到的到底是不是 div
    3. selenium 如果不靠谱, 为什么不直接拿整个 HTML 丢给自己家的 lxml 去解析
    wzwwzw
        5
    wzwwzw  
       2020-02-04 02:15:43 +08:00   ?? 1
    看看是不是没有切换 iframe。
    yezhiye
        6
    yezhiye  
    OP
       2020-02-04 08:57:07 +08:00 via Android
    感谢楼上各位答复。是我 xpath 写错了,已经解决问题了…
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1097 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:43 · PVG 06:43 · LAX 15:43 · JFK 18:43
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com