我正在使用 Selenium 服务器。它可以很好地测试在端口 80 上运行的应用程序。
但是如果我测试在 80 以外的其他端口上运行的应用程序,例如5001,连接被拒绝。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities;
br = webdriver.Remote(command_executor="http://localhost:4444/wd/hub", desired_capabilities=DesiredCapabilities.CHROME)
br.get("http://127.0.0.1:5001/login/")
br.get_screenshot_as_file("/tmp/test.png")
如何在端口 5001 上进行测试?
编辑
我正在使用 docker-compose 将 Selenium 服务器作为 Docker 容器运行:
version: '2'
services:
selenium:
image: selenium/standalone-chrome:latest
ports:
- 4444:4444
最佳答案
您正在 Docker 容器内运行 Selenium。如果您尝试连接到本地主机,它将指向 Docker 容器本身。
您必须连接到您的主机,如下所述:How to access host port from docker container
Use your internal IP address or connect to the special DNS name
host.docker.internal
which will resolve to the internal IP address used by the host.
关于如果测试的应用程序在 80 以外的端口上运行,Selenium 会拒绝连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51195637/