zmzimpl/chrome-power-app

多进程窗口selenium调试端口只有1个

LuisRain opened this issue · 3 comments

Describe the bug
新建两个窗口,API打开之后返回的调试端口port为顺序递增,比如9222,9223分别是两个窗口返回的调试port。
但是两个窗口其中webSocketDebuggerUrl 返回的地址端口却是一模一样的。
这样会导致多进程调试无法处理,因为都是用的一样的地址,且port其实也是未生效的

To Reproduce
1、新建多个窗口
2、使用selenium进行多个窗口同时调试访问不同的页面

image
image
同时打开两个进程,port 怎么会是一样的的?使用这类自动化工具的时候,应该先调用接口唤起窗口,然后用 selenium 连接,页面是否相同和 port 会不会被重复利用没关系。

参考以下群友的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from chromedriver_py import binary_path
import requests

id = "2"
open_url = f"http://127.0.0.1:49156/profiles/browser?windowId={id}"
res = requests.get(open_url).json()
debug_port = res['window']['port']
service=Service(executable_path=binary_path)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", f'127.0.0.1:{debug_port}')
driver = webdriver.Chrome(service=service,options=options)
driver.get('https://www.baidu.com/')

image image 同时打开两个进程,port 怎么会是一样的的?使用这类自动化工具的时候,应该先调用接口唤起窗口,然后用 selenium 连接,页面是否相同和 port 会不会被重复利用没关系。

参考以下群友的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from chromedriver_py import binary_path
import requests

id = "2"
open_url = f"http://127.0.0.1:49156/profiles/browser?windowId={id}"
res = requests.get(open_url).json()
debug_port = res['window']['port']
service=Service(executable_path=binary_path)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", f'127.0.0.1:{debug_port}')
driver = webdriver.Chrome(service=service,options=options)
driver.get('https://www.baidu.com/')

群友的代码是单线程没有并发。
这边并发后port有可能会一样,有可能是顺序递增的。
然后你的意思实现该功能是:如果要多个窗口同时操作,就顺序打开10个窗口,然后再并发去driver连接? 这个我也试了,单线程顺序打开也会端口一致
这是我这边多进程的代码:

def get_driver_with_port(port):

    return driver, wait

def go(item):
    ct = requests.get("http://localhost:49156/profiles/browser?windowId=" + str(item["id"]))
    js = json.loads(ct.content)
    w_port = js["window"]["port"]
    print("调用id:{}返回的结果: {}".format(item["id"],js))
    driver, wait = get_driver_with_port(w_port)

    driver.get("http://google.com")
    time.sleep(20)


ct = requests.get("http://localhost:49156/profiles")
js = json.loads(ct.content)
threadNum = 10
pool = ThreadPool(threadNum)
pool.map(go, js)
pool.close()
pool.join()

然后日志如图:
20240401093222

image image 同时打开两个进程,port 怎么会是一样的的?使用这类自动化工具的时候,应该先调用接口唤起窗口,然后用 selenium 连接,页面是否相同和 port 会不会被重复利用没关系。
参考以下群友的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from chromedriver_py import binary_path
import requests

id = "2"
open_url = f"http://127.0.0.1:49156/profiles/browser?windowId={id}"
res = requests.get(open_url).json()
debug_port = res['window']['port']
service=Service(executable_path=binary_path)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", f'127.0.0.1:{debug_port}')
driver = webdriver.Chrome(service=service,options=options)
driver.get('https://www.baidu.com/')

群友的代码是单线程没有并发。 这边并发后port有可能会一样,有可能是顺序递增的。 然后你的意思实现该功能是:如果要多个窗口同时操作,就顺序打开10个窗口,然后再并发去driver连接? 这个我也试了,单线程顺序打开也会端口一致 这是我这边多进程的代码:

def get_driver_with_port(port):

    return driver, wait

def go(item):
    ct = requests.get("http://localhost:49156/profiles/browser?windowId=" + str(item["id"]))
    js = json.loads(ct.content)
    w_port = js["window"]["port"]
    print("调用id:{}返回的结果: {}".format(item["id"],js))
    driver, wait = get_driver_with_port(w_port)

    driver.get("http://google.com")
    time.sleep(20)


ct = requests.get("http://localhost:49156/profiles")
js = json.loads(ct.content)
threadNum = 10
pool = ThreadPool(threadNum)
pool.map(go, js)
pool.close()
pool.join()

然后日志如图: 20240401093222

增加了锁,应该可以了