Selenium Webdriver - NoSuchElementExceptions [英] Selenium Webdriver - NoSuchElementExceptions

查看:28
本文介绍了Selenium Webdriver - NoSuchElementExceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 selenium webdriver 的 python 单元测试库 (unittest).我正在尝试按名称查找元素.大约有一半的时间,测试会抛出 NoSuchElementException,而其他时间不会抛出异常.

I am using the python unit testing library (unittest) with selenium webdriver. I am trying to find an element by it's name. About half of the time, the tests throw a NoSuchElementException and the other time it does not throw the exception.

我想知道这是否与 selenium webdriver 等待页面加载时间不够长有关.

I was wondering if it had to do with the selenium webdriver not waiting long enough for the page to load.

推荐答案

driver = webdriver.WhatEverBrowser()
driver.implicitly_wait(60) # This line will cause it to search for 60 seconds

它只需要在你的代码中插入一次(我通常在创建 webdriver 对象后立即插入)

it only needs to be inserted in your code once ( i usually do it right after creating webdriver object)

例如,如果您的页面由于某种原因需要 30 秒加载(购买新服务器),并且该元素是最后显示在页面上的内容之一,它几乎只是一遍又一遍地检查再次,如果元素存在 60 秒,那么如果没有找到它,它会抛出异常.

for example if your page for some reason takes 30 seconds to load ( buy a new server), and the element is one of the last things to show up on the page, it pretty much just keeps checking over and over and over again if the element is there for 60 seconds, THEN if it doesnt find it, it throws the exception.

还要确保您的范围是正确的,即:如果您专注于一个框架,而您要查找的元素不在该框架中,则它不会找到它.

also make sure your scope is correct, ie: if you are focused on a frame, and the element you are looking for is NOT in that frame, it will NOT find it.

这篇关于Selenium Webdriver - NoSuchElementExceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆