python webdriver grid多节点运行webdriver程序

grid整理:

机制

Hub机器和节点机器上要装jdk和jar包

A机器:hub  中控:用来监控所有节点机的状态

启动命令:

java -jar selenium-server-standalone-3.8.1.jar -role hub

启动成功结果显示:

INFO - Registered a node http://192.168.1.105:6666

B机器:节点机1,注册到hub机器上,角色是webdriver节点

启动节点机命令:

java -jar selenium-server-standalone-3.8.1.jar -role webdriver -hub http://127.0.0.1:4444/grid/register-Dwebdriver.chrome.driver="c:\chromedriver.exe"-Dwebdriver.ie.driver="d:\test\IEDriverServer.exe"-Dwebdriver.firefox.driver="c:\geckodriver.exe" -port 6666 -maxSession 5 -browser browserName="internet explorer",maxInstances=5 -browser browserName="chrome",maxInstances=5 -browser browserName="firefox",maxInstance=5

解释:

1-注意jar包的版本号3.8.1,要统一,且和机器上的该目录下的jar包信息一致

2-http://127.0.0.1:4444,是A机器中控机的ip和默认端口4444,后边接浏览器类型

3-"d:\chromedriver.exe" 是在节点机上的驱动路径

4-6666节点机对外服务的端口默认就是6666,5000以上都可以,没有限制

5-maxInstances=5,表示单独某个浏览器启动最多5个

6-maxSession 5,节点不同种浏览器启动的个数上限是5个,总数限制

 

注册节点机成功后提示:

The node is registered to the hub and ready to use

d机器:发起脚本执行的机器:

脚本内容:

driver = webdriver.Remote(

  # 设定Node节点的URL地址,后续将通过访问这个地址连接到Node计算机

  #节点机的ip

  command_executor = 'http://localhost:6666/wd/hub',

  desired_capabilities = {

        # 指定远程计算机执行使用的浏览器为firefox

        "browserName": "firefox",

        "video": "True",

        # 远程计算机的平台

        "platform": "WINDOWS"

        #"platform": "windows_nt"

  })

操作步骤

起hub:

java -jar selenium-server-standalone-3.8.1.jar -role hub

起节点机:三种浏览器都有

java -jar selenium-server-standalone-3.8.1.jar -role webdriver -hub http://127.0.0.1:4444/grid/register-Dwebdriver.chrome.driver="c:\chromedriver.exe"-Dwebdriver.ie.driver="d:\test\IEDriverServer.exe"-Dwebdriver.firefox.driver="c:\geckodriver.exe" -port 6666 -maxSession 5 -browser browserName="internet explorer",maxInstances=5 -browser browserName="chrome",maxInstances=5 -browser browserName="firefox",maxInstance=5

脚本:

test_grid_by_firefox.py:

#encoding=utf-8

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import time

driver = webdriver.Remote(

  # 设定Node节点的URL地址,后续将通过访问这个地址连接到Node计算机

  command_executor = 'http://localhost:6666/wd/hub',

  desired_capabilities = {

        # 指定远程计算机执行使用的浏览器为firefox

        "browserName": "firefox",

        "video": "True",

        # 远程计算机的平台

        "platform": "WINDOWS"

        #"platform": "windows_nt"

  })

print ("Video: " + "http://www.baidu.com" + driver.session_id)

try:

    #driver.implicitly_wait(30)

    driver.maximize_window()

    driver.get("http://www.sogou.com")

    assert u"搜狗" in driver.title

    elem = driver.find_element_by_id("query")

    elem.send_keys(u"webdriver实战宝典")

    elem.send_keys(Keys.RETURN)

    time.sleep(3)

    assert u"吴晓华" in driver.page_source

    print 'done!'

finally:

driver.quit()

结果:

起hub中控机:

d:\test>java -jar selenium-server-standalone-3.8.1.jar -role hub

22:27:48.714 INFO - Selenium build info: version: '3.8.1', revision: '6e95a6684b'

22:27:48.715 INFO - Launching Selenium Grid hub

2018-07-02 22:27:50.394:INFO::main: Logging initialized @2117ms to org.seleniumhq.jetty9.util.log.StdErrLog

22:27:50.420 INFO - Will listen on 4444

2018-07-02 22:27:50.554:INFO:osjs.Server:main: jetty-9.4.7.v20170914

2018-07-02 22:27:50.607:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0

2018-07-02 22:27:50.608:INFO:osjs.session:main: No SessionScavenger set, using defaults

2018-07-02 22:27:50.612:INFO:osjs.session:main: Scavenging every 600000ms

2018-07-02 22:27:50.639:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@18a70f16{/,null,AVAILABLE}

2018-07-02 22:27:50.664:INFO:osjs.AbstractConnector:main: Started ServerConnector@67e2d983{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}

2018-07-02 22:27:50.665:INFO:osjs.Server:main: Started @2389ms

22:27:50.666 INFO - Nodes should register to http://192.168.1.105:4444/grid/register/

22:27:50.666 INFO - Selenium Grid hub is up and running

22:28:13.139 WARN - Max instance not specified. Using default = 1 instance

22:28:13.147 INFO - Registered a node http://192.168.1.105:6666

起节点机:

D:\test>java -jar selenium-server-standalone-3.8.1.jar -role webdriver -hub http://127.0.0.1:4444/grid/register-Dwebdriver.chrome.driver="c:\chromedriver.exe"-Dwebdriver.ie.driver="d:\test\IEDriverServer.exe"-Dwebdriver.firefox.driver="c:\geckodriver.exe" -port 6666 -maxSession 5 -browser browserName="internet explorer",maxInstances=5 -browser browserName="chrome",maxInstances=5 -browser browserName="firefox",maxInstance=5

22:28:11.177 INFO - Selenium build info: version: '3.8.1', revision: '6e95a6684b'

22:28:11.178 INFO - Launching a Selenium Grid node

2018-07-02 22:28:12.593:INFO::main: Logging initialized @1908ms to org.seleniumhq.jetty9.util.log.StdErrLog

22:28:12.669 INFO - Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()`

22:28:12.701 INFO - Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`

22:28:12.710 INFO - Using `new EdgeOptions()` is preferred to `DesiredCapabilities.edge()`

22:28:12.712 INFO - Driver class not found: com.opera.core.systems.OperaDriver

22:28:12.713 INFO - Using `new OperaOptions()` is preferred to `DesiredCapabilities.operaBlink()`

22:28:12.715 INFO - Using `new SafariOptions()` is preferred to `DesiredCapabilities.safari()`

22:28:12.717 INFO - Driver class not found: org.openqa.selenium.phantomjs.PhantomJSDriver

22:28:12.756 INFO - Driver provider class org.openqa.selenium.safari.SafariDriver registration is skipped:

 registration capabilities Capabilities {browserName: safari, platform: MAC, version: } does not match the current platform WIN10

22:28:12.802 INFO - Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`

22:28:12.804 INFO - Using `new EdgeOptions()` is preferred to `DesiredCapabilities.edge()`

22:28:12.804 INFO - Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()`

22:28:12.805 INFO - Using `new OperaOptions()` is preferred to `DesiredCapabilities.operaBlink()`

22:28:12.805 INFO - Using `new SafariOptions()` is preferred to `DesiredCapabilities.safari()`

22:28:12.815 INFO - Using the passthrough mode handler

2018-07-02 22:28:12.851:INFO:osjs.Server:main: jetty-9.4.7.v20170914

2018-07-02 22:28:12.898:WARN:osjs.SecurityHandler:main: [email protected]@6f4a47c7{/,null,STARTING} has uncovered http methods for path: /

2018-07-02 22:28:12.908:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@6f4a47c7{/,null,AVAILABLE}

2018-07-02 22:28:12.936:INFO:osjs.AbstractConnector:main: Started ServerConnector@4c309d4d{HTTP/1.1,[http/1.1]}{0.0.0.0:6666}

2018-07-02 22:28:12.939:INFO:osjs.Server:main: Started @2253ms

22:28:12.940 INFO - Selenium Grid node is up and ready to register to the hub

22:28:12.976 INFO - Starting auto registration thread. Will try to register every 5000 ms.

22:28:12.977 INFO - Registering the node to the hub: http://127.0.0.1:4444/grid/register

22:28:13.147 INFO - The node is registered to the hub and ready to use

注册成功后

访问http://127.0.0.1:4444/grid/console可以看到注册到hub的driver情况

 

可以在view config看到配置

 

脚本结果:

D:\test>python test.py

Video: http://www.baidu.com9e11588b-935d-4a97-a92c-cef268dea154

done!

总结一下:

用grid多节点功能,需要注意的点比较多,如:

一、不同的浏览器和驱动可能运行不起来,需要自己调试哪个版本的好用,目前可以确定firefox49版本和对应驱动可以跑起来,ie和chrome还不确定哪些一定可以

二、另外注册节点机的时候,即使驱动目录不存在也会提示注册成功,要通过访问http://127.0.0.1:4444/grid/console查看注册的webdriver信息和或运行脚本程序时才能验证是否真正注册成功

三、在同一台机器上跑时,需要注意运行注册命令的目录和跑脚本的目录,不是一个的话,可能会有问题,待验证

四、grid的原理是在hub机器、节点机、跑脚本的机器是独立的机器时,跑的,此种场景需要在多台机器上验证

猜你喜欢

转载自www.cnblogs.com/xiaxiaoxu/p/9256328.html