我正在尝试使用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'
有人知道我在这里哪里出错了吗?
发布于 2015-10-18 10:00:41
问题是你的锁链。注意,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') 不幸的是,驱动程序到达方法不返回任何内容,因此不能像在django代码中那样更改它。您需要像在其他python代码中那样行。
https://stackoverflow.com/questions/33196150
复制相似问题