无浏览器的Selenium测试

65

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


可能是Is it possible to hide the browser in Selenium RC?的重复问题。 - Salvador Dali
10个回答

128

Chrome现在有一个无头模式:

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

11
这应该就是答案。 - O.rka
1
最简单的答案通常是最好的!谢谢。 - hod
2
用户警告:Selenium对PhantomJS的支持已被弃用,请改用Chrome或Firefox的无头版本。 - Tiffany F.
如果我想要将某些内容复制到剪贴板并粘贴,该怎么使用它呢?我尝试过了,但只有在浏览器打开时才有效。 - basilisk

13

由于 PhantomJS 已被弃用,使用 headless 版本的 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/')

这个程序在我的Macbook上运行。我不知道为什么它会在我的Dock右侧悄悄地打开一个Firefox图标(看起来像是空闲的图标),但是你无法看到正在发生什么。所以我猜,它确实是在无头模式下运行。 - toking

10

尝试这段代码:

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

8

要在Centos上安装(请以root权限进行所有安装)

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

python get-pip.py

安装Selenium

如果你的系统上有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)

7

我已经尝试使用Apache JMeter来进行GWT应用程序的负载测试。但效果并不十分成功。 - Mohyt

3

始终遵循文档。这是selenium文档的说法。它提供了一个独立的jar包

  • Download the standalone jar. And run it with command

    java -jar selenium-server-standalone.jar
    
  • Now you will see a stanalone server started.

  • Now set up your webdriver like below and rest part will be as it is.

    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True})
    
  • Summary code will be like.

    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()
    

2

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

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

最初的回答:

driver = webdriver.PhantomJS()

如果您现在运行代码,将不会打开任何浏览器窗口。"最初的回答"

PhantomJS的开发目前已经暂停,根据@Hunter所说,我可以确认这一点。https://phantomjs.org/ - toking

1
如果您不想打开Web浏览器,可以导入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)

并继续执行程序的其余部分。


0

要求:

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

从下面的链接下载 Chrome Driver 二进制文件并粘贴到驱动程序目录中: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()

0

你可以简单地传递一个参数 "headless" 来测试 Selenium 而不打开浏览器。

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

这段代码片段将会给你提供你想要的东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接