用Python在Selenium中进行脆性测试
我正在为一些QA自动化任务学习python和selenium。我正在处理框架的导航部分,我有一个非常不一致的测试。在测试或现场没有变化的情况下,它有时通过,有时失败。它似乎无法执行悬停操作,然后在找不到子菜单链接时引发异常 转到功能:用Python在Selenium中进行脆性测试,python,selenium,webdriver,Python,Selenium,Webdriver,我正在为一些QA自动化任务学习python和selenium。我正在处理框架的导航部分,我有一个非常不一致的测试。在测试或现场没有变化的情况下,它有时通过,有时失败。它似乎无法执行悬停操作,然后在找不到子菜单链接时引发异常 转到功能: def goto(driver, section, subsection): if subsection != "None": hover_over = driver.find_element_by_link_tex
def goto(driver, section, subsection):
if subsection != "None":
hover_over = driver.find_element_by_link_text(section)
hover = selenium.webdriver.ActionChains(driver).move_to_element(hover_over)
hover.perform()
driver.find_element_by_link_text(subsection).click()
else:
driver.find_element_by_link_text(section).click()
基本上,section变量是第一个菜单项,需要将鼠标悬停在上面才能打开子菜单。subsection变量是要单击的子菜单链接的文本
我认为它如此脆弱的唯一原因是站点响应时间太慢,但Selenium不是要等到前面的操作完成后再进行下一个操作吗 是的,这听起来像是个时间问题 让我们在单击子菜单之前添加一个:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def goto(driver, section, subsection):
if subsection != "None":
hover_over = driver.find_element_by_link_text(section)
hover = selenium.webdriver.ActionChains(driver).move_to_element(hover_over)
hover.perform()
# IMPROVEMENT HERE
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, subsection))).click()
else:
driver.find_element_by_link_text(section).click()
非常感谢,只运行了五次,没有失败!