ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) # 执行脚本 在页面中注入一段JavaScript,以便在当前选定框架的上下文中执行 (Web context). 运行 [mobile command](../../commands/mobile-command.md) (Native Context). [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ## 使用示例 ```java // Java ((JavascriptExecutor) driver).executeScript("window.setTimeout(arguments[arguments.length - 1], 500);"); ``` ```python # Python self.driver.execute_script(‘document.title’) ``` ```javascript // Javascript // webdriver.io example var result = browser.execute(function(a, b, c, d) { // browser context - you may not access client or console return a + b + c + d; }, 1, 2, 3, 4) // node.js context - client and console are available console.log(result); // outputs: 10 // wd example await driver.execute('document.title'); ``` ```ruby # Ruby # ruby_lib example execute_script("document.title") # ruby_lib_core example @driver.execute_script("document.title") ``` ```php # PHP // TODO PHP sample ``` ```csharp // C# // TODO C# sample ``` [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ## 描述 假定执行的脚本是同步的,并且评估脚本的执行结果将返回给客户端。 脚本参数定义了脚本以函数体形式执行, 该函数的返回值也将返回给客户端. 函数将调用提供的参数数组以及通过指定顺序获取的参数对象的值. 参数可以是任何JSON的基本类型, 数组, 或者 JSON 对象。 定义WebElement引用的JSON对象将转换为相应的DOM元素。 同样地, 脚本结果中的任何WebElements都将作为WebElement JSON对象返回给客户端。 (Native Context)中 `execute` , 详见 [Mobile Commands](../../commands/mobile-command.md). [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ## 支持 [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ### Appium Server |平台|Driver|平台版本|Appium版本|Driver版本| |--------|----------------|------|--------------|--------------| | iOS | [XCUITest](../../drivers/ios-xcuitest.md) | None | None | None | | | [UIAutomation](../../drivers/ios-uiautomation.md) | None | None | None | | Android | [UiAutomator2](../../drivers/android-uiautomator2.md) | None | None | None | | | [Espresso](../../drivers/android-espresso.md) | None | None | None | | | [UiAutomator](../../drivers/android-uiautomator.md) | None | None | None | | Mac | [Mac](../../drivers/mac.md) | None | None | None | | Windows | [Windows](../../drivers/windows.md) | None | None | None | [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ### Appium Clients |语言|支持版本|文档| |--------|-------|-------------| |[Java](https://github.com/appium/java-client/releases/latest)| All | [seleniumhq.github.io](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html#executeScript-java.lang.String-java.lang.Object...-) | |[Python](https://github.com/appium/python-client/releases/latest)| All | [selenium-python.readthedocs.io](http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script) | |[Javascript (WebdriverIO)](http://webdriver.io/index.html)| All | | |[Javascript (WD)](https://github.com/admc/wd/releases/latest)| All | [github.com](https://github.com/admc/wd/blob/master/lib/commands.js#L102) | |[Ruby](https://github.com/appium/ruby_lib/releases/latest)| All | [www.rubydoc.info](https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Driver:execute_script) | |[PHP](https://github.com/appium/php-client/releases/latest)| All | [github.com](https://github.com/appium/php-client/) | |[C#](https://github.com/appium/appium-dotnet-driver/releases/latest)| All | [github.com](https://github.com/appium/appium-dotnet-driver/) | [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ## HTTP API 规范 [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ### 路径 `POST /session/:session_id/execute` [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ### URL 参数 |名称|描述| |----|-----------| |session_id|将指令发往的会话(session)的ID| [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ### JSON 参数 |名称|类型|描述| |----|----|-----------| | script | `string` | 执行脚本 | | args | `array` | 脚本参数 | [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ### 响应 脚本结果 (`any`) [//]: # (DO NOT EDIT THIS FILE! This is an auto-generated file. Editing for this document happens in /commands-yml/commands/web/execute.yml) ## 参考 * [W3C Specification](https://www.w3.org/TR/webdriver/#dfn-execute-script) * [JSONWP Specification](https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#sessionsessionidexecute)