I'm trying to access an existing selenium browser session from a separate python process. I'm able to get this working within the same python script, but when I break the reuse logic out to a separate script, it fails with the error message:
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
Here's the code that attempts to access the existing session from a separate script (this is the code that generates the error). Right now I'm updating the session_id and executor values each time manually:
""" 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()
This is the code that sets up the initial browser session:
""" 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()
Here's the script that successfully alters the existing browser window, however this is from within the same python script:
""" 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()