原文出处:https://blog.csdn.net/real_tino/article/details/53467406
按步骤操作:
1.下载selenium server,本人用的selenium版本是3.141.0(可以在cmd窗口用命令查询:pip show selenium),所以下载对应selenium-server-standalone-3.141.0.jar
2.grid配置文件,该文件负责提供主机和浏览器信息
def grid():
d = {'http://127.0.0.1:4444/wd/hub' : 'firefox','http://127.0.0.1:5555/wd/hub' : 'chrome',
}
return d
3.下载完成后进入相应的目录中打开cmd,输入java -jar selenium-server-standalone-3.141.0.jar -role hub,这是万恶之源,先启它才能干后面的事。启动之后再打开一个cmd,输入java -jar selenium-server-standalone-3.141.0.jar -role node -port 5555,这是启动第一个node,指定端口5555,与grid配置文件中所写的端口一致。
4.测试脚本# encoding:utf-8
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time,os.path
import grid_module
for host,browser in grid_module.grid().items():
driver = webdriver.Remote(
command_executor=host,
desired_capabilities={
'platform':'ANY',
'browserName':browser,
'version':'',
'javascriptEnabled':True
}
)
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys(u"中国")
driver.find_element_by_id("su").click()
time.sleep(3)
if driver.title == u"中国_百度搜索":
print("title匹配")
else:
print("title不匹配")
driver.close()
5.在浏览器中打开:http://127.0.0.1:4444/grid/console,显示好下图所示就说明启动成功
6.执行测试脚本查看是否能启动配置的两个浏览器
我配置了firefox和chrome两个浏览器,firefox能正常启动,但chrome启动报异常,还没有找到解决办法
title匹配
Traceback (most recent call last):
File "F:/HQL/源码学习/selenium Gird2/testcase.py", line 15, in <module>
'javascriptEnabled':True
File "E:pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "E:pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "E:pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "E:pythonlibsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to create new service: ChromeDriverService
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:22:52'
System info: host: 'USER-20150818GG', ip: '172.16.13.49', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_91'
Driver info: driver.version: unknown
Stacktrace: