web-dev-qa-db-ja.com

Seleniumブラウザーセッションを再利用する方法

別のpythonプロセスから既存のSeleniumブラウザセッションにアクセスしようとしています。これを同じpythonスクリプト内で機能させることができますが、再利用ロジックを別のスクリプトに分割すると、エラーメッセージが表示されて失敗します。

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 936, in connect
    (self.Host,self.port), self.timeout, self.source_address)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 722, in create_connection
    raise err
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

別のスクリプトから既存のセッションにアクセスしようとするコードは次のとおりです(これがエラーを生成するコードです)。現在、私は毎回手動でsession_idとexecutorの値を更新しています:

""" module docstring """
import time
from Selenium import webdriver

def main():
  """ reuse window in different scripts """
  # driver = webdriver.Chrome()
  session_id = '7b10acc2c99d90a68fecb71e5e481c0f'
  # executor_url = 'http://127.0.0.1:9515'
  executor_url = 'http://127.0.0.1:54467'

  print(session_id)
  print(executor_url)

  driver2 = webdriver.Remote(command_executor=executor_url,
                             desired_capabilities={})

  print('driver instance created')
  driver2.session_id = session_id
  print(driver2.current_url)
  driver2.get('http://www.yahoo.com')

  time.sleep(10)

if __name__ == '__main__':
  main()

これは、初期ブラウザセッションをセットアップするコードです。

""" module docstring """
import time
from Selenium import webdriver

def main():
  """ reuse window in different scripts """
  driver = webdriver.Chrome()
  executor_url = driver.command_executor._url # pylint: disable=W0212
  session_id = driver.session_id
  driver.get("http://tarunlalwani.com")

  print(session_id)
  print(executor_url)

  time.sleep(300)

if __name__ == '__main__':
  main()

既存のブラウザウィンドウを正常に変更するスクリプトを次に示しますが、これは同じpythonスクリプト内からのものです。

""" module docstring """
import time
from Selenium import webdriver

def main():
  """ reuse window in same script """
  driver = webdriver.Chrome()
  executor_url = driver.command_executor._url # pylint: disable=W0212
  session_id = driver.session_id
  driver.get("http://tarunlalwani.com")

  print(session_id)
  print(executor_url)

  driver2 = webdriver.Remote(command_executor=executor_url,
                             desired_capabilities={})
  driver2.session_id = session_id
  print(driver2.current_url)
  driver2.get('http://www.yahoo.com')

  time.sleep(300)

if __name__ == '__main__':
  main()
5
opike

1ファイルソリューションを使用した例を次に示しますが、2ファイルソリューションでも機能します。

from Selenium import webdriver
from Selenium.webdriver.chrome.options import Options
from multiprocessing import Process
import time

# The main process calls this function to create the driver instance.
def createDriverInstance():
    options = Options()
    options.add_argument('--disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, port=9515)
    return driver

# Called by the second process only.
def secondProcess(executor_url, session_id):
    options = Options()
    options.add_argument("--disable-infobars")
    options.add_argument("--enable-file-cookies")
    capabilities = options.to_capabilities()
    same_driver = webdriver.Remote(command_executor=executor_url, desired_capabilities=capabilities)
    same_driver.close()
    same_driver.session_id = session_id
    same_driver.get("https:wikipedia.org")
    time.sleep(4)
    same_driver.quit()

if __== '__main__':
    driver = createDriverInstance()
    driver.get("https://google.com")
    time.sleep(2)

    # Pass the driver session and command_executor to the second process.
    p = Process(target=secondProcess, args=(driver.command_executor._url,driver.session_id))
    p.start()
2
Ron Norris