2016-05-31 72 views
0

test.py硒+的Xvfb併發問題

#!/usr/bin/env python2 
import os 
from pyvirtualdisplay import Display 
from selenium import webdriver 
import sys 

display = Display(visible=0, size=(800, 600)) 
display.start() 
try: 
    capabilities = webdriver.common.desired_capabilities.DesiredCapabilities.CHROME 
    browser = webdriver.Chrome('/opt/chrome-driver/chromedriver', desired_capabilities=capabilities) 
    try: 
     browser.get('http://example.com') 
     browser.execute_script('return 1 + 1;') 
    finally: 
     browser.quit() 
finally: 
    display.stop() 
print 'Done' 

然後運行

seq 1 20 | parallel -j 5 ./test.py 

我得到的錯誤的這樣一個隨機數:

Traceback (most recent call last): 
    File "./test.py", line 15, in <module> 
    browser.execute_script('return 1 + 1;') 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute_script 
    {'script': script, 'args':converted_args})['value'] 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: chrome not reachable 
    (Session info: chrome=50.0.2661.86) 
    (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 3.19.0-59-generic x86_64) 

如果我刪除併發性(parallel -j 1),它的工作原理。

如果我刪除Xvfb,它的工作原理。

這個錯誤是什麼意思,我該如何解決它(不放棄併發或虛擬顯示)?

回答

0

看起來像開始xvfb啓動時出現併發問題。 xvfb-run unreliable when multiple instances invoked in parallel

所以我可以通過要求連續啓動xfvb會話來解決這個問題。

#!/usr/bin/env python2 
import fcntl 
import os 
from pyvirtualdisplay import Display 
from selenium import webdriver 
import sys 
import xvfbwrapper 

display = None 
browser = None 
try: 
    with open('/tmp/xvfb.lock', 'w') as lock: 
     fcntl.lockf(lock, fcntl.LOCK_EX) 
     display = Display(visible=0, size=(800, 600)) 
     display.start() 
     capabilities = webdriver.common.desired_capabilities.DesiredCapabilities.CHROME 
     browser = webdriver.Chrome('/opt/chrome-driver/chromedriver', desired_capabilities=capabilities) 
    browser.get('http://example.com') 
    browser.execute_script('return 1 + 1;') 
finally: 
    if browser: 
     browser.quit() 
    if display: 
     display.stop() 
print 'Done' 

只要每個人都使用/tmp/xvfb.lock來控制啓動的Xvfb會議開始,這個工程。

P.S.我不認爲硒驅動程序也需要被序列化,但我遇到了一個我認爲已經被修復的短暫問題。