简体   繁体   English

Python Selenium-我正在尝试使用pytest框架,遇到错误

[英]Python Selenium - I'm trying to use pytest framework , running into errors

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

from commonPages.LoginPage import LoginPage
from util.InitialSetUp import InitSetup

i = InitSetup()
chrome_options = i.close_popup()
driver = webdriver.Chrome(chrome_options=chrome_options)

log = LoginPage(driver)
log.nav_login_page()

class Test_Home_Page:
    def test_logo_exists(self):
        logo = driver.find_element(By.CLASS_NAME, 'logo')
        assert logo

    def test_user_profile_section_exists(self):
        user_profile_section = driver.find_element(By.CLASS_NAME, 'user-name')
        assert user_profile_section

    def test_user_profile_link_click_opens_dropdown(self):
        user_profile_link = driver.find_element(By.CLASS_NAME, 'user-dropdown-button')
        user_profile_link.click()

        user_profile_menu_item = driver.find_element(By.CLASS_NAME, 'user-profile-menu-item')
        assert user_profile_menu_item

        driver.close()

When run the above program, the program fails when function #2 starts : def test_user_profile_section_exists(self): 当运行上述程序时,该程序在功能2启动时失败: def test_user_profile_section_exists(self):

It throws an error: 它抛出一个错误:

self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x03AB28D0> response = {'sessionId': '57da643e085012ed03d18a284c063c24', 'status': 7, 'value': {'message': 'no such element: Unable to locate...r info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64)'}}

It executes perfectly, when there is only one function in the program. 当程序中只有一个功能时,它可以完美执行。

You are getting this error because that class element really doesn't exists or not visible. 您收到此错误的原因是该类元素确实不存在或不可见。 Again driver.find_element(By.CLASS_NAME, 'user-name') is not Boolean type, so you cannot use assert on that directly. 同样, driver.find_element(By.CLASS_NAME, 'user-name')不是布尔类型,因此您不能直接在其上使用assert

You can use below approach for tests validation for all tests. 您可以使用以下方法对所有测试进行测试验证。

def test_user_profile_section_exists(self):
    try:
        driver.find_element(By.CLASS_NAME, 'user-name')
    except NoSuchElementException:
        assert False

    assert True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Selenium 2与Python绑定,但我收到导入错误 - Trying to use Selenium 2 with Python bindings, but I'm getting an import error 我正在尝试在 Pycharm 中使用 Selenium webdriver,您可以在您的项目中使用它还是仅在 Python 控制台中使用它? - I'm trying to use Selenium webdriver in Pycharm, can you use it in your project or only Python console? 我正在尝试使用 windows 批处理文件来运行 python 代码,但是我遇到了以下错误: - I'm trying to use a windows batch file to run python code, however I am encountering the following errors: 我正在尝试在 powershell 中使用 python - I'm trying to use python in powershell 我正在尝试在 python 中使用多处理进行压缩 - I'm trying to use multiprocessing for compression in python 我正在尝试使用Python在Selenium中上下移动DOM - I'm trying to go up and down the DOM in Selenium with Python 我在尝试将 python 连接到 mysql 时遇到不同的错误 - I'm having different errors while trying to connect python to mysql 我们可以在 Selenium Pytest 框架中使用 API 调用吗? - Can we use API Calls in Selenium Pytest Framework? Pytest 当我尝试模拟输入时出现 AttributeError - Pytest I get AttributeError when I'm trying to mock input 我在 cmd 上运行 pytest 并且我收到 AssertionError - I am running pytest on cmd and i'm getting AssertionError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM