selenium 官网可知,selenium 是支持分布式测试的,需要部署 Grid 服务;
Selenium Grid
是 Selenium
套件的一部分,它专门用于并行运行多个测试用例在不同的浏览器、操作系统和机器上。
使用优点 :
下载地址 : https://www.seleniumhq.org/download/ ,尽量选择稳定且较新的版本。(https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar)
传到服务器:将下载好的 selenium-server-standalone-3.141.59.jar 包传输至 Linux 服务器
在 linux 主机上进入到 jar 包目录执行:
java -jar selenium-server-standalone-3.141.59.jar -role hub -host 0.0.0.0
[root@gitlab download]# java -jar selenium-server-standalone-3.141.59.jar -role hub -host 0.0.0.0
Picked up _JAVA_OPTIONS: -Xms64m -Xmx64m -Xmn32m
11:45:08.914 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
11:45:08.974 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 4444
2021-05-19 11:45:09.339:INFO::main: Logging initialized @581ms to org.seleniumhq.jetty9.util.log.StdErrLog
11:45:09.459 INFO [Hub.start] - Selenium Grid hub is up and running
11:45:09.460 INFO [Hub.start] - Nodes should register to http://172.17.0.1:4444/grid/register/
11:45:09.460 INFO [Hub.start] - Clients should connect to http://172.17.0.1:4444/wd/hub
11:46:27.116 INFO [DefaultGridRegistry.add] - Registered a node http://192.168.89.148:5555
在本地浏览器(同一局域网)访问地址 http://<hub主机ip>:4444/grid/console ,访问成功,可查看目前有没有注册 node
下载地址:https://chromedriver.chromium.org/,在本地调试好 chromedriver 能启动浏览器
错误解决办法:DevToolsActivePort file doesn't exist
在 windows 机器上进入下载好的 selenium-server-standalone-3.141.59.jar 目录,执行(主机 ip 地址要写对哈,并且是可以 互 ping 的):
java -jar selenium-server-standalone-3.141.59.jar -role node -port 5555 -hub http://192.168.88.253:4444/grid/register/
# t.py
from selenium.webdriver import Remote
from time import sleep
#这里lists的command_executor就是node节点的ip:port---->http://node的ip:node的port/wd/hub
lists={'http://192.168.89.148:5555/wd/hub':'chrome',
}
for host,browser in lists.items():
print(host,browser)
driver = Remote(command_executor=host,
desired_capabilities={'browserName': browser,
'platform': 'ANY',
'version':'',
'javascriptEnabled':True
}
)
driver.get("http://www.baidu.com")
driver.maximize_window()
sleep(1)
driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys(browser)
sleep(1)
driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
sleep(3)
result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
sleep(1)
assert "百度为您找到相关结果约" in result_text
sleep(1)
driver.quit()
liunx 主机上执行:python3 t.py 就会看到节点机器在运行