使用Facebook PHP WebDriver在Selenium中执行javascript [英] Execute javascript in Selenium with Facebook PHP WebDriver
问题描述
我正在尝试遵循此文档执行一个脚本,所有我得到的是一个错误,说 executeScript
方法是未定义的。
I am trying to follow this documentation to execute a script, and all I am getting is an error saying the executeScript
method is undefined.
$this->driver->navigateTo('/');
$this->driver->clickElement('#member_opt_in + label');
$this->driver->executeScript("alert('Hi');");
该文档使用 $ session
,并说更高的页面,这是速记,但不解释任何地方什么 $ session
实际包含或如何分配。
The documentation uses $session
, and says higher up the page that this is shorthand, but doesn't explain anywhere at all what $session
actually contains or how to assign it.
推荐答案
wiki on GitHub与当前的php-webdriver库并不是最新版本,并且是指之前(2013年的前期版本)版本,但该库从头开始重写。
The wiki on GitHub is not up-to-date with the current php-webdriver library and refers to previous (pre 2013) version of it - but the library was rewritten from scratch since.
要执行Selenium命令,您需要 RemoteWebDriver
的实例。 自述中可以看到一个例子。
To execute Selenium commands you need instance of RemoteWebDriver
. An example can be seen in readme.
使用 RemoteWebDriver
实例在 $ driver
变量可以执行:
With RemoteWebDriver
instance in $driver
variable you can execute:
$driver->get('http://google.com');
$element = $driver->findElement(WebDriverBy::cssSelector('#member_opt_in + label'));
$elemen->click();
// Execute javascript:
$driver->executeScript('alert("Hi");');
// Or to execute the javascript as non-blocking, ie. asynchronously:
$driver->executeAsyncScript('alert("Hi");');
请参阅 API文档以获取更多信息。
Refer to API documentation for more information.
这篇关于使用Facebook PHP WebDriver在Selenium中执行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!