【问题标题】:Python Selenium stale element exceptionPython Selenium 陈旧元素异常
【发布时间】:2015-08-22 16:56:30
【问题描述】:

我学习 Django 和 TDD。有代码(功能测试)验证输入表单位于中心。

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class NewVisitorTest(LiveServerTestCase):
"""
Test Class
"""
def setUp(self):
    self.browser = webdriver.Chrome()
    self.browser.implicitly_wait(3)

def tearDown(self):
    self.browser.quit()

def test_layout_and_styling(self):
    self.browser.get(self.live_server_url)
    self.browser.set_window_size(1024, 768)

    inputbox = self.browser.find_element_by_id('id_new_item')
    self.assertAlmostEqual(
        inputbox.location['x'] + inputbox.size['width'] / 2,
        512, delta=10
    )

    inputbox.send_keys('testing\n')
    inputbox = self.browser.find_element_by_id('id_new_item')
    self.assertAlmostEqual(
        inputbox.location['x'] + inputbox.size['width'] / 2,
        512, delta=10
    )

发生错误:

======================================================================
ERROR: test_layout_and_styling (functional_tests.tests.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "e:\Education\ttd\superlists\functional_tests\tests.py", line 112, in tes
t_layout_and_styling
inputbox.location['x'] + inputbox.size['width'] / 2,
  File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\we
belement.py", line 358, in location
old_loc = self._execute(Command.GET_ELEMENT_LOCATION)['value']
  File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\we
belement.py", line 448, in _execute
return self._parent.execute(command, params)
  File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\we
bdriver.py", line 196, in execute
self.error_handler.check_response(response)
  File "c:\Program Files\Python34\lib\site-packages\selenium\webdriver\remote\er
rorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale elemen
t reference: element is not attached to the page document
  (Session info: chrome=44.0.2403.155)
  (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a9468
2a),platform=Windows NT 6.1 SP1 x86_64)

使用 Firefox 可以正常工作。 我也试过了:

from selenium.webdriver.support.ui import WebDriverWait
...
...
def find(driver):
    element = driver.find_elements_by_id("data")
    if element:
        return element
    else:
        return False
element = WebDriverWait(driver, secs).until(find)

另外,我尝试了:self.browser.implicitly_wait(3),但它没有帮助。我有 Selenium 和 ChromeDriver 的最新更新。请问有什么问题吗?

我在 stackoverflow 上阅读了类似的问题,但也无济于事。 谢谢!

【问题讨论】:

  • 可以提供网站的链接吗?
  • 它在我的本地主机上 :(

标签: python django selenium


【解决方案1】:

inputbox.send_keys('testing\n')

我敢打赌,您的表单正在响应换行符并自行提交。因此,当您进行断言时,浏览器中显示的页面已更改或正在更改。

【讨论】:

  • 非常感谢!没有'\n'它可以完美地工作。 )
  • @tack 很高兴能帮上忙。
猜你喜欢
  • 2017-05-28
  • 2021-10-02
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
相关资源
最近更新 更多