gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:42:28 25 4
gpt4 key购买 nike

我已经用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)

关于python - 如何使用 Selenium Webdriver 在 Python 中执行软断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51739332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com