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
ling516
V2EX  ›  Python

Python selenium 如何实现找不到元素立即跳转下一步

  •  
  •   ling516 · 2022-01-20 07:16:05 +08:00 · 2428 次点击
    这是一个创建于 820 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python selenium 使用 for 循环执行类似页面元素查找 如何实现找不到元素立即跳转下一循环

    因为找不到元素会抛出异常导致进程结束 使用 try 也能实现这个,但是等待时间过长 能否实现找不到元素立刻进行下一个循环 节约时间

    7 条回复    2022-01-20 20:11:01 +08:00
    eel777jiang
        1
    eel777jiang  
       2022-01-20 07:41:12 +08:00 via iPhone
    可以修改 find_element 的最大等待时间
    ila
        2
    ila  
       2022-01-20 08:17:50 +08:00 via Android
    有等待的方法,不用自己写 try
    jsdtxm
        3
    jsdtxm  
       2022-01-20 08:25:14 +08:00
    显示等待,可以自行设定等待的时间,修改 timeout 参数即可。
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    WebDriverWait(self.driver, timeout=10).until(EC.visibility_of_element_located(loc))
    uqf0663
        4
    uqf0663  
       2022-01-20 08:30:18 +08:00
    没用过 Python selenium ,但是很好奇 if dom.length=0 不行?
    Jwyt
        5
    Jwyt  
       2022-01-20 09:40:26 +08:00
    @uqf0663 因为要等待页面加载
    pckillers
        6
    pckillers  
       2022-01-20 11:54:53 +08:00
    加个 s , 用 find_elements

    找不到元素时就会反馈空数组。判断数组为空时跳下一个业务,数组不为空时取数组第一个值进行业务。
    ling516
        7
    ling516  
    OP
       2022-01-20 20:11:01 +08:00
    发现问题所在了 隐式等待时间过长造成的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2826 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:30 · PVG 19:30 · LAX 04:30 · JFK 07:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.