【问题标题】:How to check if an html element is attached to the page document如何检查html元素是否附加到页面文档
【发布时间】:2021-11-27 15:04:05
【问题描述】:

所以我在 python 中使用 selenium 循环来不断检查 html 元素列表的属性,但是偶尔会从文档中分离出一个元素,并在其位置添加另一个元素。当我尝试检查已分离元素的属性时,它会抛出错误“过时的元素引用”。我已经对其进行了搜索,并从 selenium 中找到了“NoSuchElementException”,据说这可以帮助我避免这个问题,但是我不知道谁来正确地实现它。 “NoSuchElementException”的任何其他解决方案/帮助都会很棒:D

【问题讨论】:

    标签: python selenium selenium-webdriver nosuchelementexception


    【解决方案1】:

    基本上,您希望将抛出错误的代码包装在 try except 语句中。像这样的smthn

     try:
         #this will always run
         #some code - eg. check element attributes
     except NoSuchElementException:
         # this will be executed if NoSuchElementException error is raised by code in try block
         # here you can do whatever you need
    

    【讨论】:

    • 嗨,谢谢你的帮助,我刚刚注意到当我的案例需要 StaleElementReferenceException 时我使用了 NoSuchElementException,谢谢你的帮助:D
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2015-02-08
    • 1970-01-01
    • 2013-06-19
    • 2020-11-09
    相关资源
    最近更新 更多