使用Watir与PhantomJS返回空响应,请求技术修复方案
Hey there, let's figure out why your script is returning an empty response and get it working (or switch to a more reliable alternative, since PhantomJS is long deprecated). Here are the key fixes and recommendations:
1. Wait for the Page to Fully Load
The most common reason for an empty browser.html is that you're trying to grab the page source before PhantomJS finishes loading it. Add explicit waits to ensure the page is ready:
require 'watir' OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE browser = Watir::Browser.new :phantomjs browser.goto "https://google.com" # Wait until the page's ready state is marked complete Watir::Wait.until { browser.document.ready_state == 'complete' } # Alternatively, wait for a core page element to confirm loading browser.wait_until { browser.text_field(name: 'q').exists? } puts browser.html
2. Tweak PhantomJS Compatibility & Configuration
PhantomJS hasn't been updated since 2017, so it struggles with modern websites and SSL setups. Try these adjustments:
- Use the last stable PhantomJS version (2.1.1) – older builds have critical SSL and JavaScript parsing bugs.
- Pass command-line arguments to handle SSL and mimic a real browser (many sites block PhantomJS as a bot):
browser = Watir::Browser.new :phantomjs, args: [ '--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1.2', '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ]
3. Ditch PhantomJS (Highly Recommended!)
PhantomJS is officially dead and unsupported. Modern browsers like Chrome/Edge have robust headless modes that work seamlessly with Watir, and they support all modern web features. Here's a drop-in replacement using Chrome Headless:
require 'watir' # Configure Chrome headless options options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless=new') # Use Chrome's updated, more stable headless mode options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') # Required for some environments like Linux servers browser = Watir::Browser.new :chrome, options: options browser.goto "https://google.com" Watir::Wait.until { browser.document.ready_state == 'complete' } puts browser.html
This will be far more reliable for most modern websites.
Why Your Original Script Failed
Chances are one of these issues was causing the empty response:
- PhantomJS couldn't load Google's modern JavaScript fast enough before you called
browser.html, - Google rejected PhantomJS's outdated user-agent or SSL configuration,
- Or PhantomJS's outdated webkit engine couldn't parse the page correctly.
内容的提问来源于stack exchange,提问作者Muhammad Adeel

