Python使用Selenium测试WebApp在定位元素和使用元素属性报的莫名其妙错误的终极解决方法

时间:2025-01-27 14:57:43

1   : Message: Element is not currently visible and so may not be interacted with...

2   : Message: Element not found in the cache - perhaps the page has changed since it was looked up

...

...


以上就是差不多的错误了,其实Selenium定位元素和使用元素都是使用Javascript来实现的,很多时候是因为页面数据动态更新了,导致元素的位置发生变化,所以Javascript就无法定位元素,也许可以认为,是在特定时间内找不到指定的元素了,所以会报 timeout 错误。


我的解决方法就是,死循环!!!!


while True:
    try:
        driver.find_element_by_id('button').click()
        break
    except ElementNotVisibleException: # here you can add the sepcific exception 
        print "ElementNotVisibleException"