测试人社区

python +appium ,在查找元素时,appium报错UnexpectedAlertOpenError后,程序就停止了

Matched JSONWP error code 7 to NoSuchElementError
[info] e[35m[HTTP]e[39m e[37m<-- POST /wd/hub/session/57bc791a-b528-4ccc-bd91-c4b8d6661921/element e[39me[31m500e[39m e[90m112 ms - 347e[39m
[info] e[35m[HTTP]e[39m e[90me[39m[info] e[35m[HTTP]e[39m e[37m–>e[39m e[37mPOSTe[39m e[37m/wd/hub/session/57bc791a-b528-4ccc-bd91-c4b8d6661921/elemente[39m
[info] e[35m[HTTP]e[39m e[90m{“using”:“class name”,“value”:“home-title”,“sessionId”:“57bc791a-b528-4ccc-bd91-c4b8d6661921”}e[39m
[info] e[35m[MJSONWP]e[39m Driver proxy active, passing request on via HTTP proxy
[debug] e[35m[JSONWP Proxy]e[39m Matched ‘/wd/hub/session/57bc791a-b528-4ccc-bd91-c4b8d6661921/element’ to command name ‘findElement’
[debug] e[35m[JSONWP Proxy]e[39m Proxying [POST /wd/hub/session/57bc791a-b528-4ccc-bd91-c4b8d6661921/element] to [POST http://127.0.0.1:8000/wd/hub/session/dec70ef8a3674896a9d188e003e263f4/element] with body: {“using”:“class name”,“value”:“home-title”,“sessionId”:“57bc791a-b528-4ccc-bd91-c4b8d6661921”}[debug] e[35m[JSONWP Proxy]e[39m Got response with status 200: {“sessionId”:“dec70ef8a3674896a9d188e003e263f4”,“status”:26,“value”:{“message”:“unexpected alert open\n (Session info: chrome=71.0.3578.99)\n (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17763 x86_64)”}}
[debug] e[35m[MJSONWP]e[39m Encountered internal error running command: ProxyRequestError: Could not proxy command to remote server. Original error: The request to /wd/hub/session/57bc791a-b528-4ccc-bd91-c4b8d6661921/element has failed
[debug] e[35m[MJSONWP]e[39m at JWProxy.proxy$ (C:\Users\admin\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:182:13)
[debug] e[35m[MJSONWP]e[39m at tryCatch (C:\Users\admin\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
[debug] e[35m[MJSONWP]e[39m at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\admin\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
[debug] e[35m[MJSONWP]e[39m at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\admin\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
[debug] e[35m[MJSONWP]e[39m at GeneratorFunctionPrototype.invoke (C:\Users\admin\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
[debug] e[35m[MJSONWP]e[39m at
[debug] e[35m[MJSONWP]e[39m Matched JSONWP error code 26 to UnexpectedAlertOpenError
[info] e[35m[HTTP]e[39m e[37m<-- POST /wd/hub/session/57bc791a-b528-4ccc-bd91-c4b8d6661921/element e[39me[31m500e[39m e[90m404 ms - 271e[39m
[info] e[35m[HTTP]e[39m e[90me[39m

场景:APP,操作登陆时,切换到了H5页面,点击登陆后,查找登陆成功后页面的一个元素,此时并不存在弹窗。
第一个报错是页面没刷新出来,没找到元素,状态码是“status":7,,我设置的超时时间是30S,当时后面继续查找时,报错状态码,“status”:26,UnexpectedAlertOpenError,应该已经不是找不到元素的问题了?
此时程序会停止运行了。

这个脚本运输是有时成功有时失败。哪位大佬分析下原因,这个26的报错百度也找不到相关的信息


这里有错误的说明:打开了一个模态对话框,阻止了此操作。
猜测是权限问题,或者你确认一下页面是H5的吗?还是原生的?

登陆时输入账号密码的页面和登陆后查找元素用于断言的页面都是H5的,但是这个报错不是必现的,运行成功的概率还是大于失败的概率

是否增加了显式等待增加健壮性,用visibility_of_element_located。

贴代码看看,不然不好分析

查元素的函数:
def base_find_element(self,loc,timeout=30,poll=1.0):
return WebDriverWait(self.driver,timeout=timeout,poll_frequency=poll).until(lambda d :
d.find_element(*loc))

返回断言的函数:
def base_is_element_exist(self,loc):
try:
print(self.base_find_element(loc,timeout=10).text)
return True
except:
return False

查元素的函数:
def base_find_element(self,loc,timeout=30,poll=1.0):
return WebDriverWait(self.driver,timeout=timeout,poll_frequency=poll).until(lambda d :
d.find_element(*loc))

返回断言的函数:
def base_is_element_exist(self,loc):
try:
print(self.base_find_element(loc,timeout=10).text)
return True
except:
return False

嗯哼,其实你可以在对象层,添加响应操作的等待。
已点击举例说明:
from selenium.webdriver.support import expected_conditions as EC

def click_element(self, selector):
    selector_value = selector.split('=>')[1]
    try:
        el=WebDriverWait(self.driver, 10,2).until(EC.visibility_of_element_located((By.XPATH,selector_value)))
        logger.info("获取元素 \' %s \' 成功 ""by %s 值为: %s " % (el.text, 'xpath', selector_value))
        logger.info("该元素 \' %s \' 已被点击." % el.text)
        el.click()
    except AttributeError as e:
        logger.error("点击元素失败 %s" % e)

10秒内,没两秒检查一次页面是否有该元素;有就点击。
不过我看你的代码,感觉应该也可以;不过都试试吧~:joy: