Python Selenium Webdriver HtmlTestRunner 报告未生成

Posted

技术标签:

【中文标题】Python Selenium Webdriver HtmlTestRunner 报告未生成【英文标题】:Python Selenium Webdriver HtmlTestRunner Report Not Generated 【发布时间】:2021-03-25 16:28:01 【问题描述】:

代码如下所示

from selenium import webdriver
import unittest
import htmlTestRunner
from selenium.webdriver.common.by import By

class TestReport(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(executable_path="F:\\3-2 Projects\\PycharmProjects\\Selenium01\\drivers\\chromedriver.exe")
        cls.driver.implicitly_wait(2)

    def test_Case_1(self):
        self.driver.get("https://opensource-demo.orangehrmlive.com/")
        self.driver.find_element(By.LINK_TEXT,"Forget your password?").click()

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        print("Test Completed")

如果 name == 'ma​​in': unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='F:/3-2 Projects/PycharmProjects/Selenium01/Reports'))



Give some response












输出:

测试于晚上 10:14 开始... C:\Users\User\AppData\Local\Programs\Python\Python38\python.exe "C:\Program Files\JetBrains\PyCharm 2019.2.6\helpers\pycharm_jb_unittest_runner.py" --path "F:/3-2项目/PycharmProjects/Selenium01/ReportSelenium.py" 使用参数启动单元测试 python -m unittest F:/3-2 Projects/PycharmProjects/Selenium01/ReportSelenium.py in F:\3-2 Projects\PycharmProjects\Selenium01

错误 回溯(最近一次通话最后): 文件“C:\Users\User\AppData\Local\Programs\Python\Python38\lib\unittest\case.py”,第 60 行,在 testPartExecutor 屈服 运行中的文件“C:\Users\User\AppData\Local\Programs\Python\Python38\lib\unittest\case.py”,第 676 行 self._callTestMethod(testMethod) _callTestMethod 中的文件“C:\Users\User\AppData\Local\Programs\Python\Python38\lib\unittest\case.py”,第 633 行 方法() 文件“F:\3-2 Projects\PycharmProjects\Selenium01\ReportSelenium.py”,第 16 行,在 test_Case_1 self.driver.find_element(By.LINK_TEXT,"忘记密码?").click() 文件“C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 976 行,在 find_element 返回 self.execute(Command.FIND_ELEMENT, 执行中的文件“C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 321 行 self.error_handler.check_response(响应) 文件“C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py”,第 242 行,在 check_response 引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:“方法”:“链接文本”,“选择器”:“忘记密码?” (会话信息:chrome=89.0.4389.90)

断言失败

测试完成

在 14.704 秒内运行 1 次测试

失败(错误=1)

进程以退出代码 1 结束

断言失败

断言失败

【问题讨论】:

【参考方案1】:

提供的选择器似乎没有找到元素,请尝试在下一个选择器上更改它:

//div[@id='forgotPasswordLink']/a

所以,查找元素的步骤如下:

self.driver.find_element(By.XPATH,"//div[@id='forgotPasswordLink']/a").click()

self.driver.find_element(By.ID,"forgotPasswordLink").click()

一些棘手的事情:

1.安装webdriver-manager,它可以让你自动安装和存储chromedrive

pip install webdriver-manager

并像这样使用:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

2.开始使用WebdriverWait,允许动态等待元素,一次获取:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
timeout = 30

element = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, 'forgotPasswordLink')))
element.click()

【讨论】:

以上是关于Python Selenium Webdriver HtmlTestRunner 报告未生成的主要内容,如果未能解决你的问题,请参考以下文章

Selenium WebDriver(Python)API

[python] python+selenium+webdriver

[python] python+selenium+webdriver

selenium webdriver (python)大全

python+selenium—webdriver入门

转载selenium+Python WebDriver之selenium的定位以及切换frame(iframe)