Python Selenium: Block-Title is not properly verified. (Magento Cloud)

折月煮酒 提交于 2021-02-16 11:27:52

问题


Details:

Currently I am writing within a project based on Magento Cloud test cases with Python-Selenium. So far everything is fine. Currently I only have one problem, which I can't explain anymore.

Actually it's only about the verification of a text. Or the verification of a block title within a profile page.

I would like to secure multiple times, and thus define 2 different test cases.

issues

I always get the following message.

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

The Source

#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)

Questions:

  • Do I use the correct query? Apparently no?
  • Is it due to Magento
  • How can I check these blocks?


回答1:


The relevant HTML in text format would have helped to construct a canonical answer. However, you were close. To assert the block title within the profile page you have to you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategies:

  • Using CSS_SELECTOR and text attribute:

    #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))
    
  • Using XPATH and get_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))
    
  • Note : You have to add the following imports :

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



回答2:


Please try webdriver wait for visibility of element so the element will get time to load in dom properly, it will prevent it from to become stale element.

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

        driver.get("https:my-url.de")
        wait = WebDriverWait(driver, 60)
                try: 
                   accountInfo = wait.until(ec.visibility_of_element_located((By.XPATH, "//strong[.='Account Information']")))
                   self.assertEqual("Account Information", accountInfo.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)


来源:https://stackoverflow.com/questions/59213880/python-selenium-block-title-is-not-properly-verified-magento-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!