【问题标题】:Handling staleelement reference exception Coverfox website using Python pytest selenium使用 Python pytest selenium 处理 staleelement 引用异常 Coverfox 网站
【发布时间】:2019-11-08 11:29:20
【问题描述】:

test_homepage.py的内容

    def test_insurance_pages_open_successfully_using_fixtures(page_object, load_home_page, insurance_data):
            page_object.open_insurance(insurance_data)
            assert page_object.ui.contains_text('Buying two-wheeler insurance from Coverfox is simple')

open_insurance 页面对象中的函数home_page.py

    def open_insurance(self, insurance):
        self._ui.move_to(locators.drp_dwn_insurance)
        self._ui.click(format_locator(locators.lnk_insurance, insurance))

move_to 函数在另一个 file.py

    def move_to(self, locator):
        to_element = self.find_element(locator)
        print("element value", to_element)
        self.action.move_to_element(to_element).perform()

我在这里想要做的是 test_insurance_pages_open_successfully_using_fixtures 将 3 个固定装置作为参数 1

page_object 在会话级别 2 提供页面对象。

load_home_page 在会话级别 3 再次加载主页。

insurance_data fixture in conftest.py 从某个 CSV 文件中读取的链接文本供应商列表

因此,本质上,它将加载页面并为网站一一打开所有链接 - https://www.coverfox.com/

第一个测试用例通过了链接两轮车保险,但对于第二个测试数据运行它失败了,在它试图移动到(move_to 函数)的点上给出过时元素引用的异常 再次保险链接。

我没有在任何地方存储元素,并且函数的编写方式可以再次找到该元素。

这是什么原因造成的?或者 Pytest 在后台做某种元素缓存

【问题讨论】:

  • 能否分享一下例外情况?

标签: python-3.x selenium pytest ui-automation


【解决方案1】:

看来你应该用函数级的fixture 代替load_home_page 或者在你做了一些动作之后刷新页面。

在当前的方法中(至少是您描述的方式),您将相同的页面和页面状态用于不同的测试。

能否也分享一下灯具代码?

【讨论】:

  • 我已经尝试了两种方法,但没有奏效。在一个中,我将 load_home_page 作为一个功能级别夹具。在另一次尝试中,我只做了模块级别。即使函数的编写方式也在函数体中做一个新的查找元素,理想情况下不应出现异常
  • 是的,请查找异常 E selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attach to the page document E (Session info: chrome=78.0.3904.97) E (驱动程序信息:chromedriver=78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}),platform=Windows NT 10.0.18362 x86_64) ..\venv\lib\site-packages\selenium\webdriver\remote \errorhandler.py:192:StaleElementReferenceException ========================================== ================= 1 次失败,1 次通过,3 次跳过 29.13 秒
  • 夹具代码 - @pytest.fixture(params=load_test_data("insurance_categories")) def insurance_data(request): data = request.param return data
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 2020-04-25
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多