【问题标题】:Selenium testing without browser没有浏览器的 Selenium 测试
【发布时间】:2011-11-27 11:03:44
【问题描述】:

我使用 Selenium RC 进行测试。现在要执行负载测试,我想运行并行测试用例。 有什么方法可以在不打开浏览器的情况下运行它们?

【问题讨论】:

标签: python selenium selenium-webdriver selenium-rc load-testing


【解决方案1】:

Chrome 现在有无头模式:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

【讨论】:

  • 这应该是答案
  • 最简单的答案通常是最好的!谢谢。
  • UserWarning: Selenium 对 PhantomJS 的支持已被弃用,请使用无头版本的 Chrome 或 Firefox 代替 warnings.warn('Selenium 对 PhantomJS 的支持已被弃用,请使用无头版本'
  • 如果我还想将某些内容复制到剪贴板然后粘贴,我该如何使用它?我试过了,但它只有在浏览器打开时才有效
【解决方案2】:

在 Centos 上设置(以 root 身份进行所有安装)

安装pip下载https://bootstrap.pypa.io/get-pip.py

python get-pip.py

安装硒 如果您的系统上有 pip,您可以简单地安装或升级 Python 绑定: pip install -U selenium

或者,您可以从 PyPI 下载源代码分发包(例如 selenium-2.53.1.tar.gz),解压缩并运行:

python setup.py install

安装程序:pyvirtualdisplay

pip install pyvirtualdisplay

yum install Xvfb libXfont Xorg

然后修改您的脚本,在 ** 和 ** 中添加粗体

**from pyvirtualdisplay import Display**
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class SeleniumDemo(unittest.TestCase):

    def setUp(self):
        **self.display = Display(visible=0, size=(800, 600))
        self.display.start()**
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.soastastore.com/"
        self.verificationErrors = []
        self.accept_next_alert = True


    def tearDown(self):`enter code here`
        self.driver.quit()
        ***self.display.stop()***
        self.assertEqual([], self.verificationErrors)

【讨论】:

    【解决方案3】:

    您可以无头运行 Selenium,看看这个问题/答案:Is it possible to hide the browser in Selenium RC?

    特别是对于性能负载测试,您应该看看 Apache JMeter.

    【讨论】:

    • 我已经尝试过使用 Apache JMeter 对 GWT 应用程序进行负载测试。但它并不十分成功。
    【解决方案4】:

    试试这个代码:

    op = webdriver.ChromeOptions()
    op.add_argument('headless')
    driver = webdriver.Chrome(options=op)
    

    【讨论】:

      【解决方案5】:

      由于 PhantomJS 已被弃用,使用无头版本的 Firefox 将是一个可行的选择。

      from selenium import webdriver
      from selenium.webdriver.firefox.options import Options
      
      options = Options()
      options.add_argument("--headless")
      driver = webdriver.Firefox(options=options)
      driver.get('https://www.google.com/')
      

      【讨论】:

        【解决方案6】:

        始终遵循文档。这是selenium doc 所说的。它提供了一个standalone jar

        • 下载独立的 jar。并使用命令运行它

          java -jar selenium-server-standalone.jar
          
        • 现在您将看到独立服务器已启动。

        • 现在像下面这样设置您的网络驱动程序,其余部分将保持原样。

          driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True})
          
        • 摘要代码如下。

          from selenium import webdriver
          from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
          from selenium.webdriver.common.keys import Keys
          driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', 
          desired_capabilities={'browserName': 'htmlunit', 'version': '2', 
          'javascriptEnabled': True})
          driver.get("http://www.python.org")
          assert "Python" in driver.title
          elem = driver.find_element_by_name("q")
          elem.clear()
          elem.send_keys("pycon")
          elem.send_keys(Keys.RETURN)
          assert "No results found." not in driver.page_source
          driver.close()
          

        【讨论】:

          【解决方案7】:

          这是可能的,但不能使用标准的 firefox 驱动程序 / chrome / 等。

          您需要安装 PhantomJS。只需将您的 WebDriver 分配给 phantomJS 驱动程序的实例:

          driver = webdriver.PhantomJS()
          

          如果您现在运行代码,将不会打开任何浏览器窗口。

          【讨论】:

            【解决方案8】:

            如果您不想打开网络浏览器,可以导入Options

            from selenium import webdriver   # for webdriver
            from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
            from selenium.webdriver.chrome.options import Options  # for suppressing the browser
            

            然后在代码中:

            option = webdriver.ChromeOptions()
            option.add_argument('headless')
            driver = webdriver.Chrome(options=option)
            

            然后继续程序的其余部分。

            【讨论】:

              【解决方案9】:

              要求:

              sudo apt-get install xvfb
              pip install selenium
              pip install PyVirtualDisplay
              

              从以下链接下载 chrome 驱动程序二进制文件并粘贴到驱动程序目录:https://sites.google.com/a/chromium.org/chromedriver/downloads

              代码:

              from selenium import webdriver
              from pyvirtualdisplay import Display
              
              with Display(visible=False, size=(800, 600)):
                  browser = webdriver.Chrome('drivers/chromedriver')
                  browser.get('https://www.example.com')
                  print(browser.page_source)
                  browser.quit()
              

              【讨论】:

                猜你喜欢
                • 2018-07-22
                • 2012-04-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-29
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多