首页 > 解决方案 > 重新连接硒以打开会话

问题描述

我是新来的

我在 geckobrowser 或 chrome 浏览器中重新连接会话时遇到 selenium 问题

我使用了重用表单 chromebrowser,我有这个错误

root@vps685-8lryq:/var/www/html/coin# python3 test3.py
6d77295243d88243e56f807cb8a3c381
http://127.0.0.1:40785
Traceback (most recent call last):
  File "test3.py", line 31, in <module>
    driver2 = ReuseChrome(command_executor=executor_url, session_id=session_id)
NameError: name 'ReuseChrome' is not defined

我已经安装了 chromium 和 chromebrowser 并且没有其他问题,它只是重用不起作用:/

from selenium.webdriver.chrome.options import Options
from pyvirtualdisplay import Display 

display = Display(visible=0, size=(1024, 768)) 
display.start() 

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--disable-notifications")
options.add_argument("--disable-gpu")
# The first time you use Chrome () New browser session
driver = webdriver.Chrome( chrome_options=options)

# Record executor_url and to reuse session session_id
executor_url = driver.command_executor._url
session_id = driver.session_id
# Access Baidu
driver.get("http://www.spiderpy.cn/")

print(session_id)
print(executor_url)

# If the driver object does not exist, but the browser is not closed
del driver

# Use ReuseChrome () multiplexing the last session
driver2 = ReuseChrome(command_executor=executor_url, session_id=session_id)

# Print current_url to Baidu's address, indicating reuse successful
print(driver2.current_url)
driver2.get("https://www.baidu.com") 

我测试了其他解决方案:

https://tarunlalwani.com/post/reusing-existing-browser-session-selenium/

这个解决方案工作只是我不理解所有代码,但我需要使用 2 个文件

1个原始浏览器启动文件

1 个文件用于在会话中重新连接

对不起我的英语我是法国人

帮我恳求 2 天我试着跑好这个;/

非常感谢你

标签: python-3.xselenium

解决方案


推荐阅读