细节:
目前,我正在一个基于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)
问题:
文本格式的相关HTML将有助于构建规范的答案。但是,你很亲近。要在配置文件页面中声明块标题,您需要为引入WebDriverWait,visibility_of_element_located()
并且可以使用以下定位策略:
使用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