环境
使用Selenium Grid需要java环境我的是jdk1.8,我的是Windows10系统
官网下载:
https://www.seleniumhq.org/download/
命令
然后打开cmd打开进入selenium grid jar包目录使用命令我的命令是:
启动 hub所在主机
cmd
d:
java -jar selenium-server-standalone-3.141.59.jar -role hub
启动之后访问:http://localhost:4444/grid/console,进入控制台
启动node所在主机(对应都有chrome浏览器和对应驱动)
本机:
java -Dwebdriver.chrome.driver="C:\\2services\\driver\\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -port 5556
远程机(都是Windows系统,我本机71):
java -Dwebdriver.chrome..driver="C:\\1013\\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -port 5558 -hub http://192.168.1.71:4444/grid/register
再次访问http://localhost:4444/grid/console
执行验证代码:
def getconfig():
d = {
"http://192.168.88.1:5556/wd/hub":"chrome"
,"http://192.168.1.72:5558/wd/hub":"chrome"
, "http://192.168.1.31:5559/wd/hub": "chrome"
}
return d
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
for host,browser in getconfig().items():
print(host)
print(browser)
driver = webdriver.Remote(
command_executor="http://127.0.0.1:4444/wd/hub",
desired_capabilities={'platform':'ANY',
'browserName':browser,
'vwesion':'',
'javascriptEnabled':True
}
)
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("hello")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.quit()
我就这一个模块,并没有别的
执行之后对应机器都打开浏览器执行了
注意
远程机如果安装有虚拟机,需要禁用虚拟机的网络,不然过来的都是虚拟机的ip,运行之后在远程机上根本没法执行