通过azure pipleine在docker容器内运行java selenium测试用例出现错误无法加载元素

问题描述 投票:0回答:1
This is my docker file 
# Base image with Maven and JDK
FROM maven:3.8.4-openjdk-11

# Set the working directory in the container
WORKDIR /app

# Install necessary dependencies
RUN apt-get update -y && apt-get install -y \
    wget \
    xvfb \
    curl \
    unzip \
    jq \
    libxss1 \
    libappindicator1 \
    libgconf-2-4 \
    fonts-liberation \
    libasound2 \
    libnspr4 \
    libnss3 \
    libx11-xcb1 \
    libxtst6 \
    lsb-release \
    xdg-utils \
    libgbm1 \
    libatk-bridge2.0-0 \
    libgtk-3-0 \
    libxcb-dri3-0 \
    libgl1-mesa-glx \
    libxrender1 \
    libxcomposite1 \
    libxi6 \
    libxrandr2 \
    libxcursor1 \
    libxinerama1

# Download versions JSON and install Google Chrome
RUN curl -s https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json > /tmp/versions.json && \
    CHROME_URL=$(jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chrome-linux64.zip $CHROME_URL && \
    unzip /tmp/chrome-linux64.zip -d /opt/chrome && \
    chmod +x /opt/chrome/chrome-linux64/chrome

# Install ChromeDriver
RUN CHROMEDRIVER_URL=$(jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chromedriver-linux64.zip $CHROMEDRIVER_URL && \
    unzip /tmp/chromedriver-linux64.zip -d /opt/chromedriver && \
    chmod +x /opt/chromedriver/chromedriver-linux64/chromedriver

# Set environment variables
ENV CHROME_BIN /opt/chrome/chrome-linux64/chrome
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH
ENV DISPLAY=:99

# Clean up
RUN rm /tmp/chrome-linux64.zip /tmp/chromedriver-linux64.zip /tmp/versions.json


# Default command to run tests
CMD ["sh", "-c", "Xvfb :99 -ac & mvn clean test -Dwebdriver.chrome.driver=/opt/chromedriver/chromedriver-linux64/chromedriver -Dwebdriver.chrome.whitelistedIps='' -Dchrome.options.args='--headless --disable-gpu --no-sandbox --disable-dev-shm-usage --remote-allow-origins=* --disable-software-rasterizer --disable-features=VizDisplayCompositor'"]

背景: 我在我的 azure pipleine 中使用上面的 docker 映像作为容器来运行 java selenium 测试用例。

错误:

no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='ExcelDataListOverall.asp']"}
  (Session info: chrome=129.0.6668.70)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1160.119.1.el7.x86_64', java.version: '11.0.14.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [62eccac0565dae1b9e5321e8200627f3, findElement {using=xpath, value=//a[@href='ExcelDataListOverall.asp']}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 129.0.6668.70, chrome: {chromedriverVersion: 129.0.6668.70 (df87d5cf12b1..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:41060}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:cdp: ws://localhost:41060/devtoo..., se:cdpVersion: 129.0.6668.70, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 62eccac0565dae1b9e5321e8200627f3

面临的问题:

但是当我从我的机器或其他 Windows 服务器运行时,该 href 元素就会启动。我也增加了超时时间,但它不起作用。当我拍摄快照时,我发现特定元素根本没有加载。**

Tested multiple way but failing. Any idea upon this**
java docker google-chrome selenium-webdriver selenium-chromedriver
1个回答
0
投票

我认为这里的问题不在docker中,因为如果它运行没有错误,则意味着它运行所有命令并正确安装所有内容 在这里您需要添加更多等待此元素尝试使用 Explicate 等待此特定元素 60 SC,以防该元素存在并且您可以在本地运行相同的测试..尝试使用此 xpath //a[contains (text(),'ExcelDataListOverall')]",也尝试添加另一个超时等待驱动程序。manage().timeouts().pageLoadTimeout(Duration.of Seconds(60));

另外,请确保浏览器是 hedless>true

© www.soinside.com 2019 - 2024. All rights reserved.