Selenium:Python无法从elif中找到元素

恩卡拉姆

我正在使用if-elif条件。当元素处于第一个条件时可以找到它们,但是Selenium只是挂起,而当元素在elif上时找不到。任何想法 ?

这是我正在使用的代码:

if (driver.find_elements_by_xpath(("//*[contains(text, 'Hello Automation User')]"))):
    print ("Test Passed")
elif (driver.find_elements_by_class_name('error')):
    print ("Test Failed: Username and Password combination mismatch")
else:
    print("Test failed")

仅供参考,我正在使用chromedriver

保卫

与其捕获异常,不如尝试这样:

if len(driver.find_elements_by_xpath(("//*[contains(text, 'Hello Automation User')]"))) > 0:
  # pass
elif len(driver.find_elements_by_class_name('error')) > 0:
  # fail
else:
  # fail

请注意,driver.find_elements_by_xpath返回一个数组,因此它总是真实的(您拥有它的方式)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章