【发布时间】:2015-11-14 06:19:22
【问题描述】:
当我尝试测试包含表格的网页时,我不断收到 StaleElementReferenceException。该表有点 [以及一些其他信息] 和两个单独的阻塞点的状态,每个阻塞点都有一个用于“是”和“否”的切换状态按钮。
在这个特定的代码中,过程是:
- 单击复选框以仅显示阻塞点。
- 如果表中没有阻塞点,您就完成了。否则...
- 保存第一行中点的名称并检查第一个阻塞状态。如果设置为“是”,请将其更改为“否”。
- 检查该点是否仍然存在。如果是这样,将第二个阻止状态更改为“否”并确认该点已被删除。
我在代码中添加了 cmets 以帮助遵循我的流程:
# << Setup >>
driver.get(url("/PointsTable/"))
assertExpectedConditionTrue(driver, "By.XPATH", "//td")
# < Confirm that the points blocking checkbox is enabled >
if not driver.find_element_by_id("BlockedPoints").is_selected():
assertExpectedConditionTrue(driver, "By.ID", "BlockedPoints")
driver.find_element_by_id("BlockedPoints").click()
assertCheckBoxEnabled(driver, "BlockedPoints")
# < First check if any points have a blocking state >
try:
assertExpectedConditionTrue(driver, "By.XPATH", "//td[contains(text(), 'No data available in table')]", None, 3)
except (NoSuchElementException):
# < Until all the points are out of blocking state, begin changing blocking statuses
# to all the points >
while True:
# < Check if all the points are set to have no blocking statuses set to Yes >
try:
assertExpectedConditionFalse(driver, "By.XPATH", "//td[contains(text(), 'No data available in table')]", None, 2)
except (NoSuchElementException, TimeoutException):
break
# < Save the name of the point
# Check the first blocking status. If it is blocking, set the block to No >
assertExpectedConditionTrue(driver, "By.XPATH", "//td")
myPointVal = driver.find_element_by_xpath("//td").text
try:
assertExpectedConditionTrue(driver, "By.XPATH", "//tbody/tr[1]/td[5]/div/button[@class='btn active btn-success btn-small point-button']", None, 2)
except (NoSuchElementException):
assertExpectedConditionTrue(driver, "By.XPATH", "//tbody/tr[1]/td[5]/div/button[@class='btn btn-small point-button']")
driver.find_element_by_xpath("//tbody/tr[1]/td[5]/div/button[@class='btn btn-small point-button']").click()
# < Save the name of the point again. Compare it to the original saved point
# If the name is the same, then the second blocking status needs to be set to No
# If the name is different, that means the point in question is no longer blocked >
assertExpectedConditionTrue(driver, "By.XPATH", "//td")
if myPointVal == driver.find_element_by_xpath("//td").text:
assertExpectedConditionTrue(driver, "By.XPATH", "//tbody/tr[1]/td[6]/div/button[@class='btn btn-small point-button']")
driver.find_element_by_xpath("//tbody/tr[1]/td[6]/div/button[@class='btn btn-small point-button']").click()
assertExpectedConditionFalse(driver, "By.XPATH", "//td", myPointVal)
当一个点的所有阻塞状态都被删除时,它会从表中消失,这就是我的异常的原因。代码并不总是在同一行上失败,但是当它失败时,它总是在我尝试单击“是”或“否”按钮的行上,这很可能是因为表格在一个点之后发生了变化已成功从表中删除。
i.e. driver.find_element_by_xpath("//tbody/tr[1]/td[6]/div/button[@class='btn btn-small point-button']").click()
它有时会通过这部分代码并在我尝试单击按钮之后的不同部分失败......(1)刷新页面,或(2)导航到第二页,XPATH 地址相同,但 XPATH 地址中的对象发生了变化。由于here 列出的原因,我确实理解我遇到此问题的原因。我的问题似乎与“元素不再附加到 DOM”一致。
到目前为止,我已尝试在可能导致表更改的位置同时使用 time.sleep() 和 driver.implicitly_wait(),但问题仍然存在。我该如何解决这个问题?
【问题讨论】:
标签: python selenium selenium-webdriver webdriver