Python Selenium:块标题未正确验证.(Magento 云) [英] Python Selenium: Block-Title is not properly verified. (Magento Cloud)
问题描述
详情:
目前我正在使用 Python-Selenium 编写基于 Magento Cloud 测试用例的项目.到目前为止一切都很好.目前我只有一个问题,我无法再解释了.
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.
我想多次保护,因此定义了 2 个不同的测试用例.
I would like to secure multiple times, and thus define 2 different test cases.
问题
我总是收到以下消息.
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
如何检查这些块?
- Do I use the correct query? Apparently no?
- Is it due to Magento
How can I check these blocks?
推荐答案
文本格式的相关 HTML 将有助于构建规范的答案.然而,你已经接近了.要在配置文件页面中声明块标题,您需要为 visibility_of_element_located()
引入 WebDriverWait 并且您可以使用以下 定位器策略:
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:
使用
CSS_SELECTOR
和text
属性:
#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))
使用 XPATH
和 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
这篇关于Python Selenium:块标题未正确验证.(Magento 云)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!