Selenium Grid操做使用指南

1、實現串行多瀏覽器執行腳本
一、啓動selenium-server-standalone
java -jar selenium-server-standalone-2.44.0.jar
二、腳本代碼javascript

 1 from selenium import webdriver
 2 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 3 import time
 4 
 5 lists = ["chrome","firefox"]
 6 for i in lists:
 7     print(i)
 8     driver = webdriver.Remote(
 9         command_executor="http://127.0.0.1:4444/wd/hub",
10         desired_capabilities={'platform':'ANY',
11                               'browserName':i,
12                               'vwesion':'',
13                               'javascriptEnabled':True
14                               }
15     )
16     driver.get("http://www.baidu.com")
17     driver.find_element_by_id("kw").send_keys("hello")
18     driver.find_element_by_id("su").click()
19     time.sleep(3)
20     driver.quit()

2、實現串行多節點(分佈式)執行腳本:java

一、啓動多節點selenium-server-standalone
主節點啓動(代碼所在主機)
java -jar selenium-server-standalone-2.44.0.jar -role hub
分支節點1啓動(北京機房)
java -jar selenium-server-standalone-2.44.0.jar -role node -port 5555
分支節點2啓動(上海機房)
java -jar selenium-server-standalone-2.44.0.jar -role node -port 5556
經過瀏覽器查看啓動狀態:http://代碼所在主機/grid/consolenode

以下顯示啓動成功web

配置文件代碼:chrome

1 def getconfig():
2     d = {"http://192.168.109.1:5555/wd/hub":"chrome",
3          "http://192.168.109.1:5556/wd/hub":"firefox"}
4     return d

實現代碼:瀏覽器

 1 from selenium import webdriver
 2 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 3 import time
 4 import config
 5 
 6 for host,browser in config.getconfig().items():
 7     print(host)
 8     print(browser)
 9     driver = webdriver.Remote(
10         command_executor="http://127.0.0.1:4444/wd/hub",
11         desired_capabilities={'platform':'ANY',
12                               'browserName':browser,
13                               'vwesion':'',
14                               'javascriptEnabled':True
15                               }
16     )
17     driver.get("http://www.baidu.com")
18     driver.find_element_by_id("kw").send_keys("hello")
19     driver.find_element_by_id("su").click()
20     time.sleep(3)
21     driver.quit()    

 總結:多線程

Selenium Grid並不能實現並行執行腳本,若是想並行執行須要和多線程進行結合。分佈式

相關文章
相關標籤/搜索