Jenkins执行Selenium脚本时UTF-8字符显示异常求助
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:
- Open Control Panel → Region → Administrative tab → Change system locale.
- Check the box for "Beta: Use Unicode UTF-8 for worldwide language support".
- 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:
- Edit
/etc/environmentand add these lines:LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 - Run
source /etc/environmentto apply changes immediately, or restart the node VM.
- Edit
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
- Key:
- For individual jobs, you can also set these variables in the build steps (e.g.,
export LANG=en_US.UTF-8for Linux,set LANG=en_US.UTF-8for 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

