首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用selenium测试无头firefox,但是它抛出了一个错误

使用selenium测试无头firefox,但是它抛出了一个错误
EN

Stack Overflow用户
提问于 2015-10-18 09:06:03
回答 1查看 493关注 0票数 1

我正在尝试使用Selenium测试无头firefox,下面的代码给出了正确的结果。

代码语言:javascript
复制
From a fresh Ubuntu 14.04 install I did the following

sudo apt-get install python-pip firefox xvfb
pip install selenium pyvirtualdisplay
useradd testuser
And then in a python shell:

from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Firefox()
driver.get("http://askubuntu.com")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()

但是,如果使用classDjango test.py中实现相同的功能,则无法工作并引发错误。

代码语言:javascript
复制
class FirefoxHeadlessTestCase(LiveServerTestCase):
    def setUp(self):
        # start display
        self.display = Display(visible=0, size=(1024, 768))
        self.display.start()
        # start browser
        self.driver = webdriver.Firefox()

    def tearDown(self):
        # stop browser
        self.driver.quit()
        super(FirefoxHeadlessTestCase, self).tearDown()

        # stop display
        self.display.stop()

    # check if this test should be skipped

    def test_example(self):
        # run tests
        print self.driver.get("http://askubuntu.com").page_source.encode('utf-8')

错误:print self.driver.get("http://askubuntu.com").page_source.encode('utf-8') AttributeError: 'NoneType' object has no attribute 'page_source'

有人知道我在这里哪里出错了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-18 10:00:41

问题是你的锁链。注意,django代码略有不同

代码语言:javascript
复制
 print self.driver.get("http://askubuntu.com").page_source.encode('utf-8')

从您的其他python代码

代码语言:javascript
复制
driver.get("http://askubuntu.com")
print driver.page_source.encode('utf-8')  

不幸的是,驱动程序到达方法不返回任何内容,因此不能像在django代码中那样更改它。您需要像在其他python代码中那样行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33196150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档