【问题标题】:Running Django-selenium project on server在服务器上运行 Django-selenium 项目
【发布时间】:2015-12-05 10:30:54
【问题描述】:

我在虚拟环境中开发了一个 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?如果有人有见解,请指导我.. 提前致谢。

【问题讨论】:

  • selenium 代码应该启动浏览器会话...物理上。我猜你的服务器没有这样的功能。为此,您可能需要 pyvirtualdisplay 包,但我不确定我是否正确理解了问题
  • 是的.. selenium 无法启动 firefox 会话
  • 所以,显然,您需要虚拟显示,或者您最好使用python requests 而不是selenium

标签: python django selenium web-applications server


【解决方案1】:

您的服务器似乎没有显示。 尝试查看虚拟显示器 (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()

【讨论】:

  • 我安装了 pyvirtualdisplay 并添加了您提供的代码,它给出了如下错误: ▶ Local vars /home/intern-2/newproject/newenv/Sellerworx/sellerworxapp/test.py in setUp self.display = Display(visible=0, size=(1024, 768)) ... ▶ 本地变量 /home/intern-2/newproject/newenv/Sellerworx/env/lib/python2.7/site-packages/selenium/webdriver/ firefox/webdriver.py in init self.binary, timeout),
  • 请确保您已安装任何后端:Xvfb、Xephyr、Xvnc。如果没有,您必须选择其中之一。例如,sudo apt-get install xvfb.
  • 是的,我已经安装了 xvfb。我确实做了一些研究,但最终没有任何进展。
  • @SoumiBasu 你能输入echo $DISPLAY 吗?你会得到什么?
  • 另外,你的 FirefoxBinary log_file 中有什么?
【解决方案2】:

这个解决方案帮助了我,使其工作并让硒更快。

    from pyvirtualdisplay import Display
    from selenium import webdriver

    display=Display(visible=0, size=(1325, 744))
    display.start()

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1280x1696')
    chrome_options.add_argument('--user-data-dir=/tmp/user-data')
    chrome_options.add_argument('--hide-scrollbars')
    chrome_options.add_argument('--enable-logging')
    chrome_options.add_argument('--log-level=0')
    chrome_options.add_argument('--v=99')
    chrome_options.add_argument('--multiple-process')
    chrome_options.add_argument('--data-path=/tmp/data-path')
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--homedir=/tmp')
    chrome_options.add_argument('--disk-cache-dir=/tmp/cache-dir')
    chrome_options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')

    driver=webdriver.Chrome(chrome_options=chrome_options)

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 2019-12-18
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多