python - 在服务器上运行 Django-selenium 项目

标签 python django selenium web-applications server

我在虚拟环境中开发了一个 django 项目,它在我的一个 .py 程序中使用了 selenium 模块,如下所示:

 from selenium import webdriver
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(10)
    self.base_url = "%s"%(website)
    self.verificationErrors = []
    self.accept_next_alert = True
    driver = self.driver
    driver.get(self.base_url + "/")
         driver.find_element_by_link_text("Sign In").click()
        driver.implicitly_wait(15)
        driver.switch_to_window(driver.window_handles[-1])
        driver.find_element_by_id("authentication_email").clear()
        driver.find_element_by_id("authentication_email").send_keys("%s"%(username))
        driver.find_element_by_css_selector("button.isReturning.isTransitionToSecondary").click()
        driver.find_element_by_id("authentication_password").clear()

它在我的虚拟环境中运行良好,但是当我在服务器上部署后尝试使用它时,我收到以下错误:

 WebDriverException

Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

我开始知道 selenium webdriver 只在客户端而不是服务器上运行(如果我错了请纠正我),那么有什么办法可以在服务器上执行相同的命令吗?也许导入任何新模块或调用 api?如果有人有见识,请指导我。在此先感谢。

最佳答案

看起来你的服务器没有显示。 尝试查看虚拟显示 (xvnc/xvfb)。 您也可以尝试使用 PyVirtualDisplay:https://pypi.python.org/pypi/PyVirtualDisplay 它可能看起来像这样:

from pyvirtualdisplay import Display
from selenium import webdriver

self.display = Display(visible=0, size=(1024, 768))
self.display.start()
self.driver = webdriver.Firefox()

关于python - 在服务器上运行 Django-selenium 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479813/

相关文章:

python - django - 内联 - 搜索现有记录而不是添加新记录

python - 为什么我的字符串在输出到 HTML 时附加了一个冒号

Linux Mint 上的 Django 和 PgSQL 连接问题

css - 如何使用带有 Selenium WebDriver 的 Xpath 选择元素的第 n 个子元素。 JAVA

python - 查找表格内的所有链接

python - 如何在递归字典中搜索键的值?

python - django 中请求正文为空

python - Selenium 测试: re-authenticating with BasicAuth gives an error

c# - ExecuteScript 上的 Selenium 超时

python - 从这个列表中获取 playAudio 调用的参数的优雅方法是什么