温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Python Selenium: Block-Title is not properly verified. (Magento Cloud)
magento python python-3.x selenium selenium-webdriver

其他 - Python Selenium:未正确验证Block-Title。

发布于 2020-04-20 16:32:02

细节:

目前,我正在一个基于Python-Selenium的Magento Cloud测试用例的项目中写作。到目前为止,一切都很好。目前,我只有一个问题,无法再解释了。

实际上,这仅与文本验证有关。或在个人资料页面中验证区块标题。

我想确保多次安全,因此定义了2个不同的测试用例。

问题

我总是收到以下消息。

    selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=78.0.3904.108)

来源

#Verify My Account
        driver.get("https:my-url.de")
        try: self.assertEqual("Account Information", driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='My Account'])[4]/following::strong[1]").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        self.assertEqual("Account Information", driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='My Account'])[4]/following::strong[1]").text)

问题:

  • 我使用正确的查询吗?显然不是吗?
  • 是因为Magento
  • 如何检查这些块?

    selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档

在此处输入图片说明

查看更多

提问者
Mornon
被浏览
30
DebanjanB 2020-02-07 00:52

文本格式的相关HTML将有助于构建规范的答案。但是,你很亲近。要在配置文件页面中声明块标题,您需要为引入WebDriverWaitvisibility_of_element_located()并且可以使用以下定位策略

  • 使用CSS_SELECTORtext属性:

    #Verify My Account
    driver.get("https:my-url.de")
    try: self.assertEqual("Account Information", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "main.page-main#maincontent div.block-dashboard-info > div.block-title strong"))).text)
    except (TimeoutException, AssertionError) as e: self.verificationErrors.append(str(e))
    
  • 使用XPATHget_attribute("innerHTML")

    #Verify My Account
    driver.get("https:my-url.de")
    try: self.assertEqual("Account Information", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//main[@class='page-main' and @id='maincontent']//div[@class='block-dashboard-info']/div[@class='block-title']//strong"))).get_attribute("innerHTML"))
    except (TimeoutException, AssertionError) as e: self.verificationErrors.append(str(e))
    
  • 注意:您必须添加以下导入:

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