1. <small id='xfaVf'></small><noframes id='xfaVf'>

  2. <legend id='xfaVf'><style id='xfaVf'><dir id='xfaVf'><q id='xfaVf'></q></dir></style></legend>
    <tfoot id='xfaVf'></tfoot>

      <i id='xfaVf'><tr id='xfaVf'><dt id='xfaVf'><q id='xfaVf'><span id='xfaVf'><b id='xfaVf'><form id='xfaVf'><ins id='xfaVf'></ins><ul id='xfaVf'></ul><sub id='xfaVf'></sub></form><legend id='xfaVf'></legend><bdo id='xfaVf'><pre id='xfaVf'><center id='xfaVf'></center></pre></bdo></b><th id='xfaVf'></th></span></q></dt></tr></i><div id='xfaVf'><tfoot id='xfaVf'></tfoot><dl id='xfaVf'><fieldset id='xfaVf'></fieldset></dl></div>
        <bdo id='xfaVf'></bdo><ul id='xfaVf'></ul>

      如何重用 selenium 浏览器会话

      How to reuse a selenium browser session(如何重用 selenium 浏览器会话)

      <i id='pTTLj'><tr id='pTTLj'><dt id='pTTLj'><q id='pTTLj'><span id='pTTLj'><b id='pTTLj'><form id='pTTLj'><ins id='pTTLj'></ins><ul id='pTTLj'></ul><sub id='pTTLj'></sub></form><legend id='pTTLj'></legend><bdo id='pTTLj'><pre id='pTTLj'><center id='pTTLj'></center></pre></bdo></b><th id='pTTLj'></th></span></q></dt></tr></i><div id='pTTLj'><tfoot id='pTTLj'></tfoot><dl id='pTTLj'><fieldset id='pTTLj'></fieldset></dl></div>

          <small id='pTTLj'></small><noframes id='pTTLj'>

            <tfoot id='pTTLj'></tfoot>
            • <bdo id='pTTLj'></bdo><ul id='pTTLj'></ul>

              <legend id='pTTLj'><style id='pTTLj'><dir id='pTTLj'><q id='pTTLj'></q></dir></style></legend>
                <tbody id='pTTLj'></tbody>
                本文介绍了如何重用 selenium 浏览器会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试从单独的 python 进程访问现有的 selenium 浏览器会话.我可以在同一个 python 脚本中使用它,但是当我将重用逻辑分解为一个单独的脚本时,它会失败并显示错误消息:

                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
                

                这是尝试从单独的脚本访问现有会话的代码(这是生成错误的代码).现在我每次手动更新 session_id 和 executor 值:

                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()
                

                这是成功更改现有浏览器窗口的脚本,但它来自同一个 python 脚本:

                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()
                

                推荐答案

                这是一个使用单文件解决方案的示例,尽管它也适用于双文件解决方案.

                Here is an example using a one file solution, though it would work in a two file solution as well.

                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://www.wikipedia.org")
                    time.sleep(4)
                    same_driver.quit()
                
                if __name__ == '__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()
                

                这篇关于如何重用 selenium 浏览器会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                env: python: No such file or directory(env: python: 没有这样的文件或目录)
                How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  <bdo id='LYKIx'></bdo><ul id='LYKIx'></ul>

                      <small id='LYKIx'></small><noframes id='LYKIx'>

                      • <i id='LYKIx'><tr id='LYKIx'><dt id='LYKIx'><q id='LYKIx'><span id='LYKIx'><b id='LYKIx'><form id='LYKIx'><ins id='LYKIx'></ins><ul id='LYKIx'></ul><sub id='LYKIx'></sub></form><legend id='LYKIx'></legend><bdo id='LYKIx'><pre id='LYKIx'><center id='LYKIx'></center></pre></bdo></b><th id='LYKIx'></th></span></q></dt></tr></i><div id='LYKIx'><tfoot id='LYKIx'></tfoot><dl id='LYKIx'><fieldset id='LYKIx'></fieldset></dl></div>
                            <tbody id='LYKIx'></tbody>
                        1. <legend id='LYKIx'><style id='LYKIx'><dir id='LYKIx'><q id='LYKIx'></q></dir></style></legend><tfoot id='LYKIx'></tfoot>