脚本宝典收集整理的这篇文章主要介绍了使用php webdriver从Selenium获取console.log,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我到目前为止:
$caps = DesiredCapabilITies::firefox(); $caps->setCapability('loggingPRefs',array('browser'=>'ALL')); //connect to selenium $webdriver = RemoteWebDriver::create('http://127.0.0.1:4444/wd/hub',$caps); $webdriver->get('https://www.example.COM/amp/page.htML#development=1'); sleep(10); $logs = $webdriver->manage()->getLog('browser'); var_dump($logs);
使用FaceBook的webdriver for PHP.我可以恢复日志,但它似乎没有包含来自console.log的任何内容.我该如何捕获这些数据?
$chromeCapabilities->setCapability( 'loggingPrefs',['browser' => 'ALL'] );
然后,打电话(说)
VAR_dump( $chromeDriver->manage()->getLog( 'browser' ) );
访问控制台日志.
以下是我在大约30个小时的调查后使用的工作样本 – 我希望它可以帮到某人!它可以使用或不使用Selenium,如果没有,则直接调用ChromeDriver.测试站点包含一些用于显式写入console.log的JavaScript:
<?PHP require_once (__DIR__.'/../vendor/autoload.PHP'); use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Chrome; $javaPath = '"C:\\Program Files (x86)\\Common Files\\oracle\\Java\\javapath\\java.exe"'; $seleniumPath = '"'. __DIR__ . '\\..\\selenium-server-standalone-3.141.5.jar"'; $chromeDriverPath = 'C:\path\to\chromedriver.exe'; $site = 'http://mytestsite'; $seleniumPort = 4445; $useSelenium = true; $chromeDriverPathenvVar = 'webdriver.chrome.driver'; putenv( $chromeDriverPathEnvVar .'='. $chromeDriverPath ); $chromeOptions = new Chrome\ChromeOptions(); $chromeOptions->addarguments( array( '--headless' ) ); $chromeCapabilities = DesiredCapabilities::chrome(); $chromeCapabilities->setCapability( Chrome\ChromeOptions::CAPABILITY,$chromeOptions ); $chromeCapabilities->setCapability( 'loggingPrefs',['browser' => 'ALL'] ); $selenium = null; if ($useSelenium) { $descriptorsPEc = array( 0 => array('pipe','r'),// stdin is a pipe that the child will read From 1 => array('file',__DIR__ . '/selenium_log-' . date('Ymd-His').'_'. $seleniumPort . '-stdout.txt','a'),// stdout is a pipe that the child will write to 2 => array('file',__DIR__ . '/selenium_log-' . date('Ymd-His').'_'. $seleniumPort . '-stderr.txt','a') // stderr is a file to write to ); $selenium_cmd = $javaPath .' -D'. $chromeDriverPathEnvVar .'="'. $chromeDriverPath .'" -jar '. $seleniumPath .' -port '. $seleniumPort; // If interested,add .' -debug'; $selenium = proc_open( $selenium_cmd,$descriptorspec,$pipes,null,array( 'bypass_shell' => true ) ); $host = 'http://localhost:'. $seleniumPort .'/wd/hub'; // this is the default $chromeDriver = RemoteWebDriver::create($host,$chromeCapabilities ); } else { $chromeDriver = Facebook\WebDriver\Chrome\ChromeDriver::start( $chromeCapabilities ); } $chromeDriver->get( $site ); var_dump( $chromeDriver->manage()->getLog( 'browser' ) ); $chromeDriver->quit(); sleep(1); $chromeDriver->action(); sleep(1); $chromeDriver->close(); if ($useSelenium) { fclose( $pipes[0] ); proc_terminate( $selenium ); @pclose( $selenium ); }
以上是脚本宝典为你收集整理的使用php webdriver从Selenium获取console.log全部内容,希望文章能够帮你解决使用php webdriver从Selenium获取console.log所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。