一、实现串行多浏览器执行脚本
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并不能实现并行执行脚本,如果想并行执行需要和多线程进行结合。
时间: 2024-10-12 17:00:36