背景
巨坑的一个地方,在用appium做安卓自动化的时候,界面有很多广告弹窗不定时的弹出来,就需要用到WebDriverWait方法进行等待,等不到就超时,等到了才去点击关闭,可是我这里就一直遇到设定的这个timeout不生效,不管你设定多少,他始终都是60秒才抛出错误,才开始我还以为是自己的appium和selenium的版本不兼容导致,于是我就去查看这两个版本的release note,去比对发布的日期,然后把两个都降级到差不多时间点的版本后;还是不行,然后我又在代码的各个位置加打印还是不行也没发现什么异常,我后面直接快要放弃了。终于在某一天早上我茅塞顿开,找到了一个巨坑的地方。。。。。
解决办法
巨坑的地方就是我在appnium webdriver初始化这里加了一个全局隐式等待的代码导致的!!!!
服了我自己了。
就是这个driver.implicitly_wait(60)把我坑惨了,我后面注释掉后就正常了,哎无语,
new_args = {
"deviceName":self.udid,
"platformName":platformName,
"platformVersion":platformVersion,
"appPackage":appPackage,
"appActivity":appActivity,
"automationName": "UiAutomator2",
"noRest":noRest,
"autoAcceptAlerts":"true",
"autoDismissAlerts":"true",
"udid":self.udid,
"newCommandTimeout":130 #appnium seesion会话超时的事件,建议tcpdump的值小于等于该值
}
driver = webdriver.Remote(self.ip,new_args)
# driver.implicitly_wait(60)
def wait_element_view(self, locator, locator_type="xpath", timeout=5):
"""
判断元素是否可见
:param locator: 元素定位
:param locator_type:
:param timeout: 等待的上限
:return: 可见则返回True
"""
# from selenium.common.exceptions import TimeoutException
try:
WebDriverWait(self.driver, timeout,poll_frequency=1).until(
EC.presence_of_element_located((self.by_dict[locator_type], locator)))
return True
# 如果元素出现,继续执行您的逻辑
except Exception as error:
# 如果超时,执行您的超时处理逻辑,比如返回某个值或者记录日志
print("元素可见性等待超时")
return False