Python 硒罐';t访问死对象/元素引用已过时

Python 硒罐';t访问死对象/元素引用已过时,python,django,selenium,selenium-webdriver,geckodriver,Python,Django,Selenium,Selenium Webdriver,Geckodriver,我下面将学习使用python的TDD。在执行迁移之后,命令python3 functional_tests.py的输出应该是(根据本书): 但我得到了一个错误: selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "tr" is invalid: TypeError: can't access dead object 在尝试第二次(或更多)之后: 我一直在谷歌上搜

我下面将学习使用python的TDD。在执行迁移之后,命令
python3 functional_tests.py
的输出应该是(根据本书):

但我得到了一个错误:

selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "tr" is invalid: TypeError: can't access dead object
在尝试第二次(或更多)之后:

我一直在谷歌上搜索类似的问题,但没有找到一个可以帮助我解决这个问题。
我正在使用geckodriver,并将其路径添加到
path

Django==1.8.7
selenium==3.0.2
Mozilla Firefox 50.0.2
(X)Ubuntu 16.04

我应该换成Chrome吗?这不是小事,这需要我花一些时间,但它能工作吗?更像Firefox还是Selenium?我认为这与代码无关——我克隆了,同样的崩溃也在发生。

这是因为这本书希望您使用Selenium 2,而不是Selenium 3。v3在隐式等待方面有着完全不同的行为(我上次检查时发现了相当多的bug),所以现在使用Selenium 2是最简单的


再看一下安装说明:

这是因为本书希望您使用Selenium 2,而不是Selenium 3。v3在隐式等待方面有着完全不同的行为(我上次检查时发现了相当多的bug),所以现在使用Selenium 2是最简单的


再看一下安装说明:

出现错误是因为在本章前面,我们在POST请求后添加了重定向。页面会短暂刷新,这可能会把Selenium搞砸。如果您想继续使用Selenium 3,我在这本书的博客上找到了一个解决方案:

基本上,向NewVisitorTest类添加一个方法,让您等待页面重新加载,然后继续进行断言测试

...
from contextlib import contextmanager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import staleness_of

class NewVisitorTest(unittest.TestCase):
    ...
    @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_can_start_list_and_retrieve_it_later(self):
        ...
        inputbox.send_keys("Buy peacock feathers")
        inputbox.send_keys(Keys.ENTER)

        with self.wait_for_page_load(timeout=10):
            self.check_for_row_in_list_table("1: Buy peacock feathers")

        inputbox = self.browser.find_element_by_id("id_new_item")
        inputbox.send_keys("Use peacock feathers to make a fly")
        inputbox.send_keys(Keys.ENTER)

        with self.wait_for_page_load(timeout=10):
            self.check_for_row_in_list_table("1: Buy peacock feathers")
            self.check_for_row_in_list_table("2: Use peacock feathers to make a fly")

出现错误是因为在本章前面,我们在POST请求之后添加了重定向。页面会短暂刷新,这可能会把Selenium搞砸。如果您想继续使用Selenium 3,我在这本书的博客上找到了一个解决方案:

基本上,向NewVisitorTest类添加一个方法,让您等待页面重新加载,然后继续进行断言测试

...
from contextlib import contextmanager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import staleness_of

class NewVisitorTest(unittest.TestCase):
    ...
    @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_can_start_list_and_retrieve_it_later(self):
        ...
        inputbox.send_keys("Buy peacock feathers")
        inputbox.send_keys(Keys.ENTER)

        with self.wait_for_page_load(timeout=10):
            self.check_for_row_in_list_table("1: Buy peacock feathers")

        inputbox = self.browser.find_element_by_id("id_new_item")
        inputbox.send_keys("Use peacock feathers to make a fly")
        inputbox.send_keys(Keys.ENTER)

        with self.wait_for_page_load(timeout=10):
            self.check_for_row_in_list_table("1: Buy peacock feathers")
            self.check_for_row_in_list_table("2: Use peacock feathers to make a fly")

IMHO对Chrome的支持似乎要好得多。这有点琐碎,通过apt获取chromedriver和chrome(抱歉,只是debian cmds/手边的名称)并将chrome符号链接到like/usr/bin/chrome,您就完成了(关于更改为chrome)。我认为chrome的支持似乎更好。这有点琐碎,通过apt获取chromedriver和chrome(抱歉,只是debian cmds/手边的名称)并将chrome符号链接到like/usr/bin/chrome,您就完成了(关于更改为chrome)。
...
from contextlib import contextmanager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import staleness_of

class NewVisitorTest(unittest.TestCase):
    ...
    @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_can_start_list_and_retrieve_it_later(self):
        ...
        inputbox.send_keys("Buy peacock feathers")
        inputbox.send_keys(Keys.ENTER)

        with self.wait_for_page_load(timeout=10):
            self.check_for_row_in_list_table("1: Buy peacock feathers")

        inputbox = self.browser.find_element_by_id("id_new_item")
        inputbox.send_keys("Use peacock feathers to make a fly")
        inputbox.send_keys(Keys.ENTER)

        with self.wait_for_page_load(timeout=10):
            self.check_for_row_in_list_table("1: Buy peacock feathers")
            self.check_for_row_in_list_table("2: Use peacock feathers to make a fly")