Investors were caught off guard by the sudden U.S. assault on technology giants this week, but behind the scenes, the industry’s biggest companies have been preparing for this moment of reckoning for months.

They’ve hired lawyers and built up their lobbying shops in response to antitrust investigations that have been well under way in the European Union, and which are just now getting started in Washington.

People close to the companies say they’ve long anticipated U.S. probes. And while these...