与使用客户端 javascript 代码大量实现的网站进行交互几乎是困难的,甚至是不可能的。像Goutte和BrowserKit这样的无头浏览器模拟器不知道这样的客户端代码,也无法执行它。您需要使用像Selenium或 Sahi 这样的浏览器控制器。
看看 Behat 的Mink,它为无头模拟器和成熟的浏览器控制器提供了各种驱动程序。使用它的 selenium2 驱动程序,您可以简单地与目标页面进行交互。这是一个例子:
<?php
// You need to run selenium-*.jar for this to work.
use Behat\Mink\{Mink, Session, Driver\Selenium2Driver};
$mink = new Mink([
'selenium2' => new Session(
new Selenium2Driver('firefox', null, 'http://example.com')
),
));
$page = $mink->getSession('selenium2')->getPage();
$page->findField('regoin-select-field-name')
->selectOption('target-region-value');
$page->wait(5000, 'JS code to check if the select is now populated...')
->select('city-select-field-name)
->selectOption('target-city-value');
这是未经测试的代码,但你明白了。另外,看看是如何wait()
工作的。
另外,您可能想看看facebook/php-webdriver;selenium webdriver 的 PHP 客户端,而不是使用 Mink。