Selenium在Jenkins Linux环境中页面刷新失效问题求助
Let's break down the core issues in your setup and fix them step by step:
1. Fix the Broken Linux Branch Logic in refreshIfNeeded()
Your Linux-specific code has two critical flaws that make it look like refresh isn't working:
- You're refreshing the page without first checking if
.ng-scopeelements are missing (unlike the Windows branch) - You're logging the pre-refresh element count instead of re-fetching it after refresh, which gives false negative results
Here's the corrected Linux branch:
public void refreshIfNeeded() { if(System.getProperty("os.name").equals("Linux")){ System.out.println("### A"+now()); int elementCount = driver.findElements(By.cssSelector(".ng-scope")).size(); // Only refresh if we actually need to (no elements found) if (elementCount == 0) { System.out.println("[INFO] No .ng-scope elements found, refreshing..."); // Use Selenium's native refresh method for better reliability driver.navigate().refresh(); waitForNoSpinner(); zatvoriModal(); // Re-close modal after refresh // Re-fetch element count post-refresh to verify elementCount = driver.findElements(By.cssSelector(".ng-scope")).size(); System.out.println("[DEBUG] Post-refresh .ng-scope elements: "+elementCount); } else { System.out.println("[INFO] .ng-scope elements already present, no refresh needed."); } System.out.println("### B"+now()); }else{ // Keep your existing Windows logic here System.out.println("[] Starting refreshIfNeeded()"); Date ts1 = new Date(); int count = 0; while (driver.findElements(By.cssSelector(".ng-scope")).size()==0 && count < 10){ driver.navigate().refresh(); zatvoriModal(); try { (new WebDriverWait(driver, 10)).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector(".ng-scope"), 0)); Date ts2 = new Date(); long trajanje = ts2.getTime() - ts1.getTime(); System.out.println(String.format("[INFO] Učitavanje sadržaja: %s ms.", trajanje)); } catch (Exception e){ try { Thread.sleep(3000); } catch (InterruptedException ex) { ex.printStackTrace(); } } System.out.println("[] Count: "+count); count++; } } }
2. Optimize Headless Chrome Configuration for Linux
Your current Chrome options are missing critical flags that prevent page loading issues in Jenkins/Linux environments. Update your configuration:
if(System.getProperty("os.name").equals("Linux")){ options.addArguments("--headless"); // Keep this for Chrome 77; use "--headless=new" for newer versions options.addArguments("--no-sandbox"); // Required for Jenkins (avoids permission sandbox errors) options.addArguments("--disable-dev-shm-usage"); // Fixes shared memory limits on Linux containers/VMS options.addArguments("--proxy-server='direct://'"); options.addArguments("--proxy-bypass-list=*"); options.addArguments("--window-size=1200,800"); options.addArguments("--enable-javascript"); // Ensure JS is enabled (critical for Angular apps) options.addArguments("--disable-gpu"); // Disables unnecessary GPU acceleration in headless mode WebDriverManager.chromedriver().version("77.0.3865.40").setup(); }
3. Wait for Angular to Stabilize (Not Just the Loading Spinner)
Since your app uses Angular (evidenced by .ng-scope), waiting only for the loading spinner isn't enough. Add a helper method to wait for Angular to finish rendering:
private void waitForAngular() { try { WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(driver -> { JavascriptExecutor js = (JavascriptExecutor) driver; return (Boolean) js.executeScript( "return window.angular !== undefined && " + "angular.element(document).injector().get('$http').pendingRequests.length === 0" ); }); } catch (Exception e) { // Fallback if Angular check fails System.out.println("[WARN] Angular check failed, falling back to element wait."); (new WebDriverWait(driver, 30)).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector(".ng-scope"), 0)); } }
Call this method after page load and refresh to ensure Angular has fully rendered elements.
4. Why Robot Class Isn't Working (And Why You Should Avoid It)
The Robot class relies on a graphical X11 environment, which doesn't exist in headless Jenkins setups. Even if you disable headless, Jenkins typically runs without a display. Stick to Selenium's native navigation methods—they're designed for browser automation and don't depend on GUI environments.
内容的提问来源于stack exchange,提问作者Mate Mrše

