如果测试的应用程序在 80 以外的端口上运行,Selenium 会拒绝连接

标签 selenium

我正在使用 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")

我得到以下屏幕截图: enter image description here

如何在端口 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/

相关文章:

Java Selenium : open new tab

java - 在 java selenium 中选择具有动态 id 的单选按钮

excel - 尝试使用动态下拉菜单在输入框中填充文本

c# - NUnit 无法构建测试 - 未发现测试

selenium - 如何在 Selenium WebDriver 中按向下箭头键,然后按 "Enter"按钮?

selenium - 到达错误页面 : about:neterror when trying to navigate to other tabs if there is a form submit under that tab

jquery - 使用 JQuery 选择器和 Selenium IDE 2.0.0 获取元素属性

java - 当页面左右滚动时,如何在 Selenium 中捕获整个屏幕

javascript - 如何使用selenium和scrapy来爬取某个网页?

c# - 将 SelectByText(部分)与 C# Selenium WebDriver 绑定(bind)一起使用似乎不起作用