2015-08-16 62 views
3

我使用的是Chrome浏览器驱动程序,因为Firefox驱动程序似乎在Windows PC上有问题。之前,我前去度假Django:在浏览器上使用陈旧元素参考

我的Django的功能测试中工作得很好,但现在他们都扔各种错误。

有时,试图找到一个页面上的元素,当我得到:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

其他时间,以验证该URL的尝试失败,因为硒似乎是阅读从以前的网页的URL 。失败点似乎从运行到运行都发生了变化,换句话说,测试的某些部分可以在执行之间在成功和失败之间交替。但是,这个问题似乎总是在使用.click()后发生。

在观看浏览器,硒似乎是成功的导航页面,所以我想它只是在寻找项目太quickly-在浏览器中存在才。

添加implicitly.wait()我setUpClass似乎并没有帮助,但我可能是用错了。

我试过Harry Percival's site这个很有前途的想法(下面),但是Selenium在坐在被告知要等待的页面上简单地超时。

from contextlib import contextmanager 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support.expected_conditions import \ 
    staleness_of 
class MySeleniumTest(SomeFunctionalTestClass): 
    # assumes self.browser is a selenium webdriver 

    @contextmanager 
    def wait_for_page_load(self, timeout=30): 
     old_page = self.browser.find_element_by_tag_name('html') 
     yield 
     WebDriverWait(self.browser, timeout).until(
      staleness_of(old_page) 
     ) 

    def test_stuff(self): 
     # example use 
     with self.wait_for_page_load(timeout=10): 
      self.browser.find_element_by_link_text('a link') 
      # nice! 

还有其他人处理过吗?我应该遵循这个问题的正确方法是什么?

编辑: 张贴的解决方案工作出色,真正清理我的代码,因为我可以简单地添加一个.click()到被调用的函数,就像描述的那样。

下面是帮助我自定义此文档:

Documentation for the syntax for By for modification purposes.

Documentation for Selenium's Expected Conditions

注:我用的术语“浏览器”的地方驾驶的,我认为这是最初把我扔掉。

回答

2

是否wait_for_page_load必须是生成器?我认为它会在不等待的情况下返回!这就是为什么你的测试是片状的。有时元素在您调用find_element_by_link_text时有时不会加载。

我已经用这种方法成功:

from selenium.webdriver.support import expected_conditions as EC 

def wait_for_element(self, elm, by = 'id', timeout=10) : 
    wait = WebDriverWait(self.driver, timeout) 
    if by == 'id' : 
     element = wait.until(EC.element_to_be_clickable((By.ID,elm))) 
     return self.driver.find_element_by_id(elm) 
    elif by == 'link': 
     wait.until(EC.element_to_be_clickable((By.LINK_TEXT,elm))) 
     return self.driver.find_element_by_link_text(elm) 
    # by tag, by css etc etc goes here. 

我把这种方法与应显示之前的页面可以进行交互DOM元素的一个显着标识。返回的元素可以直接与之交互。

+0

嗯,看起来我需要熟悉预期条件语法,以使其适用于我的情况。不知何故,Selenium在点击之后但在加载下一个URL之前比较URL。我试图搞砸这个,但它仍然超时,所以我不认为我正在构建我的。 –

+0

如果您用wait_for_element将您的调用替换为wait_for_page_load,我已经发布了它的工作。例如wait_for_element('链接','链接',30)应该这样做。但30秒有点太长 – e4c5

+0

道歉,我不得不离开一段时间。我已经坐下来,并得到它的工作,我很高兴我做到了!我非常感谢帮助。 –

相关问题