我有两个脚本:
sel.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
def selTry():
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_experimental_option("detach", True)
chromeOptions.add_argument('disable-infobars')
browser = webdriver.Chrome(chrome_options=chromeOptions)
executorUrl = browser.command_executor._url
sessionId = browser.session_id
print(executorUrl)
print(sessionId)
browser.get("http://www.python.org")
elem = browser.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
#existingSessionObject = webdriver.Remote(command_executor=executorUrl, desired_capabilities={})
#existingSessionObject.close()
#existingSessionObject.session_id = sessionId
#print('Sleeping...')
#time.sleep(10)
#print('Slept!')
#elem2 = existingSessionObject.find_element_by_name("q")
#elem2.clear()
#elem2.send_keys("Sec obj interacting")
#elem2.send_keys(Keys.RETURN)
if __name__ == "__main__":
selTry()
和sel2.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
def selTry2(sessionId, executorUrl):
# The sessionId and executorUrl need to be taken from the sel.py script printing.
existingSessionObject2 = webdriver.Remote(command_executor=executorUrl, desired_capabilities={})
existingSessionObject2.close()
existingSessionObject2.session_id = sessionId
print('Sleeping...')
time.sleep(10)
print('Slept!')
elem2 = existingSessionObject2.find_element_by_name("q")
elem2.clear()
elem2.send_keys("Other script interacting")
elem2.send_keys(Keys.RETURN)
if __name__ == "__main__":
selTry2('f353874fa2561c53cb971bdeed6d2e9c', 'http://127.0.0.1:41423')
如果我使用sel.py
的注释部分连接到同一脚本和函数中的现有会话,则一切正常。
但是,我要走的更远:使用sel.py
创建一个会话,将其保持打开状态,然后尝试使用sel2.py 从另一个脚本连接到该会话,只要我想
尝试时遇到的错误如下,我无法进一步理解(我已经阅读了与urllib3
错误相关的SO上的所有其他帖子,但我没有使它起作用):
Traceback (most recent call last):
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connection.py", line 159, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/util/connection.py", line 80, in create_connection
raise err
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/util/connection.py", line 70, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/http/client.py", line 1026, in _send_output
self.send(msg)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/http/client.py", line 964, in send
self.connect()
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connection.py", line 181, in connect
conn = self._new_conn()
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connection.py", line 168, in _new_conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fd340f0a160>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/eriz/Desktop/seleniumTry/sel2.py", line 23, in <module>
selTry2('f353874fa2561c53cb971bdeed6d2e9c', 'http://127.0.0.1:41423')
File "/home/eriz/Desktop/seleniumTry/sel2.py", line 7, in selTry2
existingSessionObject2 = webdriver.Remote(command_executor=executorUrl, desired_capabilities={})
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 402, in _request
resp = http.request(method, url, body=body, headers=headers)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/request.py", line 72, in request
**urlopen_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/request.py", line 150, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/poolmanager.py", line 323, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connectionpool.py", line 667, in urlopen
**response_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connectionpool.py", line 667, in urlopen
**response_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connectionpool.py", line 667, in urlopen
**response_kw)
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/home/eriz/miniconda3/envs/selenium_py/lib/python3.6/site-packages/urllib3/util/retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=41423): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd340f0a160>: Failed to establish a new connection: [Errno 111] Connection refused',))
?我要实现的目标是可行的吗?,即从另一个脚本连接到Selenium创建的会话和现有会话(一旦创建脚本完成)。
编辑:感谢您提供的参考资料 @DebanjanB 。这些天,我读到了这个问题,我想的是,现有的浏览器会话是指在Chrome中手动创建的现有浏览器,而不是以前使用Selenium启动的会话。
据我所知,即使使用基于Selenium的脚本创建会话,一旦创建脚本退出,就无法连接到该会话,对吗?
最后,如果这对其他人有所帮助,一个想法可能是基于脚本事件,以便Webdriver永远不会“死”,因为脚本不会退出,并且与我使用的现有会话进行交互不会有问题在以下脚本中执行此操作。
@DebanJanB ,如果您有任何其他建议,将不胜感激。感谢您的时间。