Python 登录页面的数据驱动测试用例
成功登录和“拒绝访问”后,登录页面显示“欢迎:)”消息失败之后。目标页面相同(表示driver.title相同)。我尝试了以下代码:Python 登录页面的数据驱动测试用例,python,selenium,Python,Selenium,成功登录和“拒绝访问”后,登录页面显示“欢迎:)”消息失败之后。目标页面相同(表示driver.title相同)。我尝试了以下代码: if driver.find_element(By.XPATH, "//h3[contains (text(),'WELCOME :)')]"): print("Testcase Pass") XLUtils.writeData(path, 'Sheet1', r, 3, "Pa
if driver.find_element(By.XPATH, "//h3[contains (text(),'WELCOME :)')]"):
print("Testcase Pass")
XLUtils.writeData(path, 'Sheet1', r, 3, "Pass")
else:
#driver.find_element(By.XPATH,"//h3[contains (text(),'ACCESS DENIED!')]")
print("Testcase Failed")
XLUtils.writeData(path, 'Sheet1', r, 3, "Failed")
driver.find_element(By.PARTIAL_LINK_TEXT, "GO BACK").click()
登录失败时,“else:”语句将不起作用,并给出以下异常。
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//h3[contains(text(),“WELCOME:)”)]“}
(会话信息:chrome=85.0.4183.121)
这是我的初级阶段:)
有没有更好的方法呢?您可以使用
尝试/除了-有关更多信息,请参阅
try:
driver.find_element(By.XPATH, "//h3[contains (text(),'WELCOME :)')]")
print("Testcase Pass")
XLUtils.writeData(path, 'Sheet1', r, 3, "Pass")
except:
print("Testcase Failed")
XLUtils.writeData(path, 'Sheet1', r, 3, "Failed")
这是一个普遍的通病。尝试步骤中的任何错误都将记录失败
进行重构以确保只有find_元素会导致失败的步骤(而不是excel)可能是值得的:
如果需要,还可以通过导入错误来限制的范围,但
除外:
from selenium.common.exceptions import NoSuchElementException
try:
# Pass conde
except NoSuchElementException:
# no such element catch code
如果您想要更多的控制,并且希望以不同的方式处理不同的异常,这将非常有用
from selenium.common.exceptions import NoSuchElementException
try:
# Pass conde
except NoSuchElementException:
# no such element catch code