如何使用 Selenium Webdriver 在 Python 中执行软断言

How to perform soft assertion in Python using Selenium Webdriver

我已经在 Python 中编写了我的 selenium 脚本,我正在通过文本验证每一页 "Home"...我正在通过硬断言来验证它,我如何通过使用软断言来修改它断言,这样如果测试失败,它不会停止我的测试脚本。 我的 selenium python 脚本是:

from selenium import webdriver
import selenium.webdriver.common.action_chains
from selenium.webdriver.common.keys import Keys



        driver = webdriver.Chrome('C:\Users\maham\Downloads\chromedriver.exe')

        driver.get("https://www.argaam.com/en")
        driver.maximize_window()


        nav_list= driver.find_elements_by_css_selector("#nav > li")
        size = nav_list.__len__()

        for i in range(0, size):
            value = driver.find_elements_by_css_selector("#nav > li")

            try:
                element_to_hover_over = value[i]
                hover = selenium.webdriver.common.action_chains.ActionChains(driver).move_to_element(element_to_hover_over)
                hover.perform()

                #drop_down_list = driver.find_elements_by_css_selector("#nav ul li")[i].find_element_by_tag_name("ul").find_element_by_class_name('holder-full').find_element_by_tag_name('li').find_elements_by_tag_name('a')
                drop_down_list = element_to_hover_over.find_elements_by_css_selector("a");
                size1 = drop_down_list.__len__()

                if size1 > 1:
                    for j in range(0, size1):
                        if j != 0:
                            value1 = driver.find_elements_by_css_selector("#nav > li")

                            element_to_hover_over = value1[i]
                            hover = selenium.webdriver.common.action_chains.ActionChains(driver).move_to_element(element_to_hover_over)
                            hover.perform()
                            value2 = drop_down_list = element_to_hover_over.find_elements_by_css_selector("a")[j];
                            value2.click();

                            ele = driver.find_element_by_css_selector("#mnu_Home > a")
                          assert ele.text == 'Home'
                else:
                    value[i].click()

            except TypeError:
                value[i].click()

而不是

assert ele.text == 'Home'

您可以使用:

try:
    assert ele.text == 'Home'
except AssertionError:
    print("Assertion failed. Actual value is %s" % ele.text)