🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> 有时候我们需要修改浏览器的一些User Agent信息或代理IP,从而达到更好的兼容性测试。这里我们就用一个在线UserAgent分析工具进行测试。 [TOC] #### 1.修改User Agent >Chrome 示例 > 这里提供user-agent大全供参考http://www.cnblogs.com/hykun/p/Ua.html ~~~ <?php namespace Facebook\WebDriver; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Chrome\ChromeOptions; require_once('vendor/autoload.php'); header("Content-Type: text/html; charset=UTF-8"); // start Firefox with 5 second timeout $host = 'http://localhost:4444/wd/hub'; // this is the default $capabilities = DesiredCapabilities::chrome(); $useragent = 'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'; $options = new ChromeOptions(); $options->addArguments(["user-agent={$useragent}"]); $capabilities->setCapability(ChromeOptions::CAPABILITY, $options); $driver = RemoteWebDriver::create($host, $capabilities, 5000); $driver->manage()->timeouts()->implicitlyWait(15); //隐性设置15秒 $driver->get('http://www.atool.org/useragent.php'); var_dump($capabilities->getCapability(ChromeOptions::CAPABILITY)); echo 'done!'; //关闭浏览器 //$driver->quit(); ?> ~~~ >Phantomjs 示例 ~~~ <?php namespace Facebook\WebDriver; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Chrome\ChromeOptions; require_once('vendor/autoload.php'); header("Content-Type: text/html; charset=UTF-8"); // start Firefox with 5 second timeout $host = 'http://localhost:4444/wd/hub'; // this is the default $capabilities = DesiredCapabilities::phantomjs(); $useragent = 'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'; $capabilities->setCapability("phantomjs.page.settings.userAgent", $useragent); $driver = RemoteWebDriver::create($host, $capabilities, 5000); $driver->manage()->timeouts()->implicitlyWait(15); //隐性设置15秒 $driver->get('http://www.atool.org/useragent.php'); echo $driver->findElement(WebDriverBy::id('ua_code'))->getAttribute('value'); echo 'done!'; //关闭浏览器 $driver->quit(); ?> ~~~ #### 2.修改代理IP ~~~ <?php namespace Facebook\WebDriver; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Remote\WebDriverCapabilityType; require_once('vendor/autoload.php'); header("Content-Type: text/html; charset=UTF-8"); $host = 'http://localhost:4444/wd/hub'; // this is the default $ip = '115.225.2.3:8998'; //设置代理IP $capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'chrome', WebDriverCapabilityType::PROXY => array('proxyType' => 'manual', 'httpProxy' => $ip, 'sslProxy' => $ip)); $driver = RemoteWebDriver::create($host, $capabilities, 5000); $driver->manage()->timeouts()->implicitlyWait(15); //隐性设置15秒 $driver->get('https://www.baidu.com/'); $driver->findElement(WebDriverBy::id('kw'))->sendKeys('ip'); $driver->findElement(WebDriverBy::id('su'))->click(); echo 'done!'; //关闭浏览器 //$driver->quit(); ?> ~~~