首页 > 解决方案 > 在 Selenium-hub(使用 docker-compose 创建)中运行 Python 测试文件时出现“TimeoutException:消息”

问题描述

我正在尝试在 Selenium Grid(使用 docker-compose.yml 文件创建)上运行我的自动化测试用例(用 Python 编写)。当我运行“docker-compose up -d”命令时,网格成功打开并按预期工作。但是,当我运行 test.py 文件(我使用的是通过我的组织发布的 VPN 连接的公司笔记本电脑)时,它不断抛出 TimeoutException: 消息。如果我在没有 DesiredCapabilities 的情况下运行 test.py 文件,它绝对可以正常工作。我第一次在我的系统(Windows 10)上使用 Selenium 网格/集线器。

我已经尽我所能在 google 和 stackoverflow 上搜索答案,但是,我来了!您能否帮助我提供反馈,因为我在哪里犯了错误?

测试.py

import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class TestLogin(unittest.TestCase):

    def setUp(self):
        base_url = "http://newtours.demoaut.com/mercurywelcome.php"
        host = "http://localhost:4444/wd/hub"
        caps = DesiredCapabilities.CHROME.copy()
        self.driver = webdriver.Remote(command_executor=host, desired_capabilities=caps)
        self.driver.maximize_window()
        self.driver.get(base_url)

    def test_login(self):
        wait = WebDriverWait(self.driver, 10)
        user_name = wait.until(ec.presence_of_element_located((By.NAME, "userName")))
        user_name.send_keys('ralphsin')
        print("Entered UserId")
        time.sleep(2)

        password = wait.until(ec.presence_of_element_located((By.NAME, "password")))
        password.send_keys('abc123')
        print("Entered Password")
        time.sleep(2)

        log_in = wait.until(ec.presence_of_element_located((By.XPATH, "//input[@alt='Sign-In']")))
        log_in.click()
        print("Clicked on Sign-In button!")
        time.sleep(2)

    def tearDown(self):
        self.driver.quit()

码头工人-compose.yml

version: '3'
services: 
    hub:
        image: selenium/hub:3.141.59
        ports: 
            - 4444:4444
    chrome:
        image: selenium/node-chrome:3.141.59
        depends_on: 
            - hub
        environment: 
            - HUB_HOST=hub
            - HUB_PORT=4444
    firefox:
        image: selenium/node-firefox:3.141.59
        depends_on: 
            - hub
        environment: 
            - HUB_HOST=hub
            - HUB_PORT=4444

错误信息

C:\Users\rsingh99\Desktop\WebDevelopment_and_Automation\Selenium_Projects\test_selenium>pytest -v test.py
=========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.6.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- c:\users\rsingh99\appdata\local\programs\python\python36\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\rsingh99\Desktop\WebDevelopment_and_Automation\Selenium_Projects\test_selenium
plugins: ordering-0.6
collected 1 item

test.py::TestLogin::test_login FAILED                                                                                                                                [100%]

================================================================================ FAILURES =================================================================================
__________________________________________________________________________ TestLogin.test_login ___________________________________________________________________________

self = <test.TestLogin testMethod=test_login>

    def test_login(self):
        wait = WebDriverWait(self.driver, 10)
>       user_name = wait.until(ec.presence_of_element_located((By.NAME, "userName")))

test.py:22:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.support.wait.WebDriverWait (session="54f19a6e911494f8fd102e2f51cb4104")>
method = <selenium.webdriver.support.expected_conditions.presence_of_element_located object at 0x000002925F4AE4E0>, message = ''

    def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
>       raise TimeoutException(message, screen, stacktrace)
E       selenium.common.exceptions.TimeoutException: Message:

..\..\..\..\appdata\local\programs\python\python36\lib\site-packages\selenium\webdriver\support\wait.py:80: TimeoutException
======================================================================== 1 failed in 11.75 seconds ========================================================================

标签: pythondockerselenium

解决方案


推荐阅读