我正在尝试使用Selenium测试无头的Firefox,下面的代码给出了正确的结果。
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()
但如果使用class
Django test.py
中的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'
错误:
log_response
任何人都知道我在哪里错了?
答案 0 :(得分:1)
问题在于你的链接。请注意您的django代码略有不同
print self.driver.get("http://askubuntu.com").page_source.encode('utf-8')
来自你的其他python代码
driver.get("http://askubuntu.com")
print driver.page_source.encode('utf-8')
不幸的是,驱动程序get方法没有返回任何内容,因此无法像在django代码中那样进行更改。您将需要像在其他python代码中那样使用行。