Selenium Grid操作使用指南
阿新 • • 發佈:2017-08-13
browser rem onf ont cap turn and 分布式 127.0.0.1
一、實現串行多瀏覽器執行腳本
1、啟動selenium-server-standalone
java -jar selenium-server-standalone-2.44.0.jar
2、腳本代碼
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()
二、實現串行多節點(分布式)執行腳本:
1、啟動多節點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/console
如下顯示啟動成功
配置文件代碼:
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並不能實現並行執行腳本,如果想並行執行需要和多線程進行結合。
Selenium Grid操作使用指南