MDN Node.js版Selenium示例无法运行,寻求技术解决方案
Hey there! Let’s dig into why your google_test.js script isn’t running as expected. Since you didn’t share the exact error message you’re getting, I’ll cover the most common pitfalls with this setup and how to fix them, plus steps to narrow down the issue further.
Common Issues & Fixes
1. Driver-Browser Version Mismatch
This is the #1 culprit for Selenium failures. ChromeDriver must match your installed Chrome version, and GeckoDriver must match your Firefox version.
- To check your browser version:
- Chrome: Go to
chrome://version/ - Firefox: Go to Help > About Firefox
- Chrome: Go to
- Download the exact matching driver version from the official driver repositories (look for ChromeDriver/GeckoDriver official downloads and pick the version aligned with your browser)
- Replace the old driver file in your PATH with the new one.
2. PATH Environment Variable Not Taking Effect
Even if you added the driver folder to your PATH, your terminal might not have picked up the change yet:
- Restart your terminal first—this fixes most PATH-related issues.
- If that doesn’t work, you can explicitly specify the driver path in your script instead of relying on PATH. Here’s how to modify the MDN example for Chrome:
const { Builder, By, Key, until } = require('selenium-webdriver'); const chrome = require('selenium-webdriver/chrome'); async function example() { let driver = await new Builder() .forBrowser('chrome') .setChromeService(new chrome.ServiceBuilder('/full/path/to/your/chromedriver').build()) .build(); try { await driver.get('http://www.google.com/ncr'); await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN); await driver.wait(until.titleIs('webdriver - Google Search'), 1000); } finally { await driver.quit(); } } example(); - For Firefox, use the
firefoxmodule andServiceBuildersimilarly.
3. Dependency Compatibility Problems
Sometimes the latest selenium-webdriver version has bugs or doesn’t play nice with newer drivers. Try pinning to a stable version:
npm uninstall selenium-webdriver npm install selenium-webdriver@4.15.0
Also, make sure you’re using a Node.js LTS version (v18 or v20 are recommended)—old Node versions can cause unexpected errors with modern packages.
4. Permissions or System Lock Issues
- Linux/macOS: Your driver files need executable permissions. Run these commands in the folder where your drivers are stored:
chmod +x chromedriver chmod +x geckodriver - Windows: If you downloaded the driver as a zip file, right-click the driver executable > Properties > Check "Unblock" (if the option exists) and click Apply. Windows sometimes blocks downloaded executable files by default.
Next Steps to Debug
If none of these fix the issue, share the full error message you see when running node google_test.js. For example:
- Is it a "driver not found" error?
- A timeout waiting for the page to load?
- An error about not being able to connect to the browser?
The exact error text will help zero in on the problem quickly.
内容的提问来源于stack exchange,提问作者H. Sock

