首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重用selenium浏览器会话

如何重用selenium浏览器会话
EN

Stack Overflow用户
提问于 2018-04-11 09:04:18
回答 2查看 17.5K关注 0票数 11

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

代码语言:javascript
复制
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值:

代码语言:javascript
复制
""" 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()

以下是设置初始浏览器会话的代码:

代码语言:javascript
复制
""" 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脚本:

代码语言:javascript
复制
""" 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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-11 10:37:43

这是一个使用一个文件解决方案的示例,尽管它在两个文件解决方案中也可以工作。

代码语言:javascript
复制
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()
票数 11
EN

Stack Overflow用户

发布于 2022-01-29 09:14:36

如果您想要终极性能,您可以将Selenium设置为接收命令的HTTP服务器。示例:

代码语言:javascript
复制
# Created by BaiJiFeiLong@gmail.com at 2022/1/29 11:58

import threading

import flask
import requests
import selenium.webdriver

app = flask.Flask(__name__)


@app.post("/")
def home():
    return str(driver.execute_script(flask.request.data.decode()))


threading.Thread(target=lambda: app.run(host="127.0.0.1", port=5000)).start()
driver = selenium.webdriver.Chrome()

print("1+1=", requests.post("http://localhost:5000", "return 1+1").text)
print("userAgent=", requests.post("http://localhost:5000", "return navigator.userAgent").text)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49764902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档