You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins执行Selenium脚本时UTF-8字符显示异常求助

Fixing UTF-8 Garbled Characters in Selenium Grid + Jenkins Setup

Hey there, let's work through this UTF-8 encoding issue you're hitting—those weird [Term�kadatok] garbled characters are definitely frustrating. Since you've already set your text file and POM to UTF-8, the problem is likely coming from mismatched encoding settings across your Jenkins environment, Selenium Grid nodes, or the underlying VM systems. Here's what you need to check and fix:

1. Configure Node VM System Encoding

Your Selenium Grid nodes' host VMs need to use UTF-8 as their default system encoding. Here's how to set it up for common OSes:

  • Windows:
    1. Open Control Panel → Region → Administrative tab → Change system locale.
    2. Check the box for "Beta: Use Unicode UTF-8 for worldwide language support".
    3. Restart the VM to apply changes.
    • Alternatively, if you can't restart, run chcp 65001 (the UTF-8 code page) in the command prompt before starting the Selenium node.
  • Linux:
    1. Edit /etc/environment and add these lines:
      LC_ALL=en_US.UTF-8
      LANG=en_US.UTF-8
      
    2. Run source /etc/environment to apply changes immediately, or restart the node VM.

2. Set Jenkins Environment Variables

Ensure Jenkins itself runs with UTF-8 encoding:

  • Go to Jenkins → Manage Jenkins → Configure System.
  • Under Global properties, check "Environment variables" and add these entries:
    • Key: JAVA_TOOL_OPTIONS, Value: -Dfile.encoding=UTF-8
    • Key: LANG, Value: en_US.UTF-8
  • For individual jobs, you can also set these variables in the build steps (e.g., export LANG=en_US.UTF-8 for Linux, set LANG=en_US.UTF-8 for Windows) before executing your Selenium scripts.

3. Enforce UTF-8 in Selenium Node Startup

When launching your Selenium Grid nodes, explicitly pass the UTF-8 encoding parameter to the JVM:

java -Dfile.encoding=UTF-8 -jar selenium-server-standalone.jar -role node -hub http://<your-hub-url>:4444/grid/register

This makes sure the node's Java process uses UTF-8 to handle all text operations.

4. Adjust Browser Encoding Preferences

Some browsers might not default to UTF-8, so configure their startup settings:

  • Chrome: Add these arguments when initializing the driver:
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--default-charset=utf-8");
    WebDriver driver = new RemoteWebDriver(new URL(gridUrl), options);
    
  • Firefox: Set encoding-related preferences:
    FirefoxOptions options = new FirefoxOptions();
    options.addPreference("intl.accept_languages", "en-US,en");
    options.addPreference("general.useragent.locale", "en-US");
    WebDriver driver = new RemoteWebDriver(new URL(gridUrl), options);
    

5. Simplify Text Extraction Logic

Double-check your code to make sure you're not overcomplicating text extraction. Avoid unnecessary encoding conversions like:

// Don't do this—this can corrupt UTF-8 characters
String garbledText = new String(driver.getText().getBytes(), "UTF-8");

Instead, directly use the string returned by driver.getText():

String correctText = driver.getText();

Most of the time, aligning the encoding settings across the VM system, Jenkins environment, and Selenium node JVM will fix this garbled character issue. Start with the VM system encoding, since that's a common overlooked factor.

内容的提问来源于stack exchange,提问作者Nihar Arisal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:32:31