1. 程式人生 > >selenium Grid配置多個節點執行多個瀏覽器

selenium Grid配置多個節點執行多個瀏覽器

java -jar {path}/selenium-server-standalone-3.8.1.jar -role hub (預設埠4444

3、啟動節點

啟動本機節點:

java -jar {path}/selenium-server-standalone-3.8.1.jar -role node  -hub http://localhost:4444/grid/register(預設埠5555)
java -jar {path}/selenium-server-standalone-3.8.1.jar  -port 4445 -role node  -hub http://localhost:4444/grid/register((指定埠)

在其他電腦上啟動節點:

java -jar {path}/selenium-server-standalone-3.8.1.jar -port 4452 -role node -hub http://{啟動hub的機器的IP}:4444/grid/register
java -jar {path}/selenium-server-standalone-3.8.1.jar -port 4453 -role node -hub http://{啟動hub的機器的IP}:4444/grid/register

mac電腦檢視本機正在使用的ip可通過在終端輸入【ifconfig | grep “inet ” | grep -v 127.0.0.1】檢視

這裡寫圖片描述

測試程式碼,執行chrome,Firefox瀏覽器:

from selenium import webdriver
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
list={'http://127.0.0.1:5555/wd/hub':'chrome',
      'http://127.0.0.1:4445/wd/hub':'firefox'}

for host,browser in list.items():
    print(host,browser)
    driver=webdriver.Remote(command_executor=host,
                            desired_capabilities={'platform'
:'ANY', 'browserName':browser, 'version':'', 'javascriptEnabled':True}) driver.get('http://www.baidu.com') driver.find_element_by_id('kw').send_keys(browser) driver.find_element_by_id('su').click() driver.close()

多執行緒啟動瀏覽器例項

#-*- coding:utf-8 -*-
from selenium import webdriver
from time import sleep
from threading import Thread

def test_baidu_search(host, browser):
    driver = None
    driver=webdriver.Remote(command_executor=host,
                            desired_capabilities={'platform':'ANY',
                                                    'browserName':browser,
                                                    'version':'',
                                                    'javascriptEnabled':True})
    if driver == None:
        exit()
    driver.get('http://www.baidu.com')
    driver.find_element_by_id('kw').send_keys(browser)
    driver.find_element_by_id('su').click()
    driver.quit()


if __name__ == "__main__":
    # 瀏覽器和首頁url
    data = {'http://127.0.0.1:5555/wd/hub':'chrome',
      'http://127.0.0.1:4445/wd/hub':'chrome'}
    # 構建執行緒
    threads = []
    for host, browser in data.items():
        t = Thread(target=test_baidu_search,args=(host,browser))
        threads.append(t)

    # 啟動所有執行緒
    for thr in threads:
        thr.start()