Django:浏览中的硒过时元素参考 [英] Django: Selenium- stale element reference on browse

查看:126
本文介绍了Django:浏览中的硒过时元素参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Chrome Webdriver,因为Firefox Webdriver在Windows PC上似乎有问题。

I am using Chrome webdriver, because the Firefox webdriver seems to have issues on my Windows PC.

在我休假之前,我的Django功能测试工作正常,但是现在他们抛出了各种错误。

My Django Functional Tests were working just fine before I went on vacation, but now they are throwing all kinds of errors.

有时,当试图在页面上查找元素时,我得到了:

Sometimes, when trying to find an element on a page I get:

selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档

在其他情况下,尝试验证URL失败,因为Selenium似乎正在从上一页读取URL。故障点似乎在每次运行之间都在变化,换句话说,测试的某些部分可以在执行之间成功与失败之间交替。但是,使用 .click()后似乎总是会出现此问题。

Other times, an attempt to verify the url fails, because Selenium seems to be reading the URL from the previous page. The failure point seems to change from run to run, in other words, certain parts of the test can alternate between successful and unsuccessful between executions. However, the issue always seems to occur after using .click().

在观看浏览器时,Selenium似乎才能成功导航到该页面,所以我想它只是在寻找项目之前太快了-在它们存在于浏览器中之前。

When watching the browser, Selenium seems to be navigating to the page successfully, so I suppose it is simply looking for items too quickly- before they exist in the browser.

隐式添加我的setUpClass的.wait()似乎无济于事,但我可能用错了。

Adding implicitly.wait() to my setUpClass doesn't seem to help, but I might be using it wrong.

我尝试了哈里·珀西瓦尔(Harry Percival)的网站,但Selenium只是在坐在被告知等待的页面上只是超时。

I tried the promising idea (below) from Harry Percival's site, but Selenium simply times out while sitting on the page that it was told to wait for.

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!

还有其他人处理过吗?我应该遵循的解决此问题的适当方法是什么?

Has anyone else dealt with this? What is the proper approach to this problem that I should be following?

编辑:
发布的解决方案非常出色,确实可以清理我的代码,因为我能够像描述的那样简单地将.click()添加到调用的函数中。

The posted solution works brilliantly and really cleaned up my code, as I am able to simply add a .click() to the called function, just as described.

以下是帮助我自定义此功能的文档:

Here are the docs that helped me customize this:

语法的文档进行修改。

Documentation for the syntax for By for modification purposes.

有关硒的预期条件的文档

注意:我使用的术语是浏览器代替驱动程序,我认为那是最初让我失望的。

Note: I was using the term "browser" in place of driver, I think that is what was initially throwing me off.

推荐答案

wait_for_page_load是否必须是生成器?我认为它会不等待而返回!这就是为什么您的测试不稳定。

Does wait_for_page_load have to be a generator? I think it would return without waiting! That's why your tests are flaky. Sometimes the element would have loaded by the time you call find_element_by_link_text sometimes not.

我成功地使用了这种方法:

I have used this approach with success:

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元素的显着ID,该元素应在页面出现之前显示与之互动。返回的元素可以直接与之交互。

I call this method with a prominent id of a dom element that should show up before the page can be interacted with. The returned element can be interacted with directly.

这篇关于Django:浏览中的硒过时元素参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆