python webdriver grid多節點運行webdriver程序

grid整理:

機制

Hub機器和節點機器上要裝jdk和jar包java

A機器:hub  中控:用來監控全部節點機的狀態node

啓動命令:python

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

 

啓動成功結果顯示:chrome

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

 

B機器:節點機1,註冊到hub機器上,角色是webdriver節點瀏覽器

啓動節點機命令:session

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=5ide

解釋:ui

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: ServletContext@o.s.j.s.ServletContextHandler@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多節點功能,須要注意的點比較多,如:

1、不一樣的瀏覽器和驅動可能運行不起來,須要本身調試哪一個版本的好用,目前能夠肯定firefox49版本和對應驅動能夠跑起來,ie和chrome還不肯定哪些必定能夠

2、另外註冊節點機的時候,即便驅動目錄不存在也會提示註冊成功,要經過訪問http://127.0.0.1:4444/grid/console查看註冊的webdriver信息和或運行腳本程序時才能驗證是否真正註冊成功

3、在同一臺機器上跑時,須要注意運行註冊命令的目錄和跑腳本的目錄,不是一個的話,可能會有問題,待驗證

4、grid的原理是在hub機器、節點機、跑腳本的機器是獨立的機器時,跑的,此種場景須要在多臺機器上驗證

相關文章
相關標籤/搜索