Python Selenium:Block-Title 未正确验证。 (Magento 云)
Python Selenium: Block-Title is not properly verified. (Magento Cloud)
详情:
目前我正在一个基于 Magento Cloud 测试用例的项目中编写 Python-Selenium。到目前为止一切都很好。目前我只有一个问题,我无法再解释了。
其实只是验证一段文字而已。或者验证个人资料页面中的块标题。
我想保护多次,因此定义了 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
如何检查这些块?
请尝试 webdriver 等待元素的可见性,以便元素有时间正确加载 dom,这将防止它成为陈旧的元素。
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)
文本格式的相关 HTML 将有助于构建规范的答案。但是,你很接近。要在个人资料页面中断言块标题,您需要为 visibility_of_element_located()
引入 WebDriverWait 并且您可以使用以下 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))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
详情:
目前我正在一个基于 Magento Cloud 测试用例的项目中编写 Python-Selenium。到目前为止一切都很好。目前我只有一个问题,我无法再解释了。
其实只是验证一段文字而已。或者验证个人资料页面中的块标题。
我想保护多次,因此定义了 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
如何检查这些块?
请尝试 webdriver 等待元素的可见性,以便元素有时间正确加载 dom,这将防止它成为陈旧的元素。
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)
文本格式的相关 HTML 将有助于构建规范的答案。但是,你很接近。要在个人资料页面中断言块标题,您需要为 visibility_of_element_located()
引入 WebDriverWait 并且您可以使用以下 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))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC