java - Chrome 驱动程序尝试在 CI 服务器中加载页面时超时,但在本地工作

标签 java selenium webdriver selenium-chromedriver google-chrome-headless

我的前端自动化套件在 Selenium Cucumber 中运行,不幸的是 chrome 驱动程序无法找到该元素。服务器有网络连接,我可以从 CI 服务器 ping url。

            [14:35:40][Step 1/1]       org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of Proxy element for: DefaultElementLocator 'By.id: login_email' (tried for 60 second(s) with 500 milliseconds interval)
            [14:35:40][Step 1/1]        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
            [14:35:40][Step 1/1]        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
            [14:35:40][Step 1/1]        at stepdefs.LoginSteps.iFillEmailWithValidEmailAddress(LoginSteps.java:115)
            [14:35:40][Step 1/1]        at ?.When I fill email with valid OTA email address(login.feature:6)
            [14:35:40][Step 1/1]       Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"login_email"}
            [14:35:40][Step 1/1]         (Session info: headless chrome=69.0.3497.100)
            [14:35:40][Step 1/1]         (Driver info: chromedriver=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 4.9.0-6-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)
            [14:35:40][Step 1/1]       Command duration or timeout: 0 milliseconds
            [14:35:40][Step 1/1]       For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
            [14:35:40][Step 1/1]       Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
            [14:35:40][Step 1/1]       System info: host: 'intelligence-teamcity', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.0-6-amd64', java.version: '1.8.0_181'
            [14:35:40][Step 1/1]       Driver info: org.openqa.selenium.chrome.ChromeDriver
            [14:35:40][Step 1/1]       Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.42.591071 (0b695ff80972cc..., userDataDir: /appdata/TeamCity/buildAgen...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:20897}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 69.0.3497.100, webStorageEnabled: true}
            [14:35:40][Step 1/1]       Session ID: d4f4672a9d49645e1bc8af8b54627aae

下面是我创建 Chrome 驱动程序实例的方式。

System.setProperty("webdriver.chrome.driver",
            "browser-drivers/chromedriver");
final ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--no-sandbox");
    chromeOptions.addArguments("--allow-running-insecure-content");
    chromeOptions.addArguments("window-size=1920x1080");
    chromeOptions.addArguments("--disable-gpu");
    chromeOptions.addArguments("--disable-extensions");
    chromeOptions.setExperimentalOption("useAutomationExtension", false);
    chromeOptions.addArguments("--proxy-server='direct://'");
    chromeOptions.addArguments("--proxy-bypass-list=*");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

    chromeDriver = new ChromeDriver(capabilities);

我在这里做错了什么?为什么它在我的而不是在服务器上工作(它也安装了 Chrome)?任何帮助将不胜感激。

更新:当它与 UI 一起运行时,它工作正常。只是 headless 是行不通的

最佳答案

貌似headless模式不支持不安全的证书,然后页面就是打不开,只打开一个空白页面,里面什么也找不到。

除了 chromeOptions 之外,只需将这两个功能添加到 DesiredCapabilities 对象:

capabilities.setCapability("acceptSslCerts", true);
capabilities.setCapability("acceptInsecureCerts", true);

关于java - Chrome 驱动程序尝试在 CI 服务器中加载页面时超时,但在本地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52439682/

相关文章:

java - 如何将单个单词作为输入句子中的单独单词匹配到只有一个单词的字符串中

java - 从字符串变量 Java 进行转换

java - 当从外部资源更新表时,Spring Data JPA 不会刷新或清除缓存

java - 在 Selenium 的单个窗口中更改不同页面之间的驱动程序焦点

Python - 断言页面中不应存在的文本字段

java - Selenium java 浏览器 session 重用

java - WebDriver FireFoxProfile UserAgent 与 FireFoxDriver 切换

java - HTTP 请求是通过 TCP 还是 UDP 接收的?

java - Selenium Java Chrome 在页面上的元素被禁用时发送 Enter 键

c# - Specflow:为许多场景大纲维护一个示例表