--- title: WebDriver slug: Web/WebDriver tags: - WebDriver translation_of: Web/WebDriver --- <p>WebDriver 是远程控制接口,可以对用户代理进行控制. 它提供了一个平台和语言中性线协议,作为进程外程序远程指导web浏览器行为的方法</p> <p>能够编写可在不同平台上的许多浏览器中交替运行的指令集,对于向用户提供一致的体验至关重要。随着web平台上新一轮的开发浪潮、设备的多样化以及对技术之间真正的互操作性的需求,WebDriver为跨浏览器测试提供了工具。</p> <p>提供了一组接口,用于发现和操作web文档中的DOM元素,并控制用户代理的行为。它的主要目的是允许web作者编写从单独的控制过程中自动执行用户代理的测试,但也可以这样使用,允许浏览器内脚本控制一个(可能是单独的)浏览器。</p> <h2 id="概念">概念</h2> <p>要使用WebDriver,您需要 ... 提供如何使用它的高级描述,而不是低级的细节,然后链接到提供安装细节的其他页面。</p> <p>WebDriver主要涉及四个方面:</p> <ul> <li>a</li> <li>b</li> <li>c</li> <li>d</li> </ul> <p>这些东西一起工作就像 ... 解释命令、动作等的工作流程。</p> <h2 id="使用">使用</h2> <p>那么WebDriver允许你做什么?它看起来是什么样子?因为WebDriver是编程语言中立的,所以这个问题的答案取决于您使用的是哪个WebDriver客户端和语言的选择。</p> <p>但是使用Python编写的流行客户端,您与WebDriver的交互可能如下所示:</p> <pre class="notranslate">from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.expected_conditions import presence_of_element_located wait = WebDriverWait(driver, 10) with webdriver.Firefox() as driver: driver.get("http://google.com/ncr") driver.find_element_by_name("q").send_keys("cheese" + Keys.RETURN) wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>a"))) results = driver.find_elements_by_css_selector("h3>a") for i, result in results.iteritems(): print("#{}: {} ({})".format(i, result.text, result.get_property("href")))</pre> <p>这可能会产生与此类似的输出:</p> <pre class="notranslate">#1 Cheese - Wikipedia (https://en.wikipedia.org/wiki/Cheese) </pre> <h2 id="Reference">Reference</h2> <h3 id="Commands"><a href="/en-US/docs/Web/WebDriver/Commands">Commands</a></h3> <p>{{ListSubpages("/en-US/docs/Web/WebDriver/Commands")}}</p> <h3 id="Types"><a href="/en-US/docs/Web/WebDriver/Types">Types</a></h3> <ul> <li><a href="/en-US/docs/Web/WebDriver/Errors#payload">Error object</a></li> <li><a href="/en-US/docs/Web/WebDriver/Timeouts">Timeouts object</a></li> <li><a href="/en-US/docs/Web/WebDriver/WebElement">WebElement</a></li> <li><a href="/en-US/docs/Web/WebDriver/WebWindow">WebWindow</a></li> </ul> <h3 id="Capabilities"><a href="/en-US/docs/Web/WebDriver/Capabilities">Capabilities</a></h3> <p>{{ListSubpages("/en-US/docs/Web/WebDriver/Capabilities")}}</p> <h3 id="Errors"><a href="/en-US/docs/Web/WebDriver/Errors">Errors</a></h3> <p>{{ListSubpages("/en-US/docs/Web/WebDriver/Errors")}}</p> <ul> </ul> <h2 id="Tutorials">Tutorials</h2> <p>List of links to tutorials? We could delete this for now, or link to tutorial elsewhere, until ours are written.</p> <h2 id="Examples">Examples</h2> <p>Include list of links to examples, preferrably in different languages. Don't include actual code blocks here.</p> <h2 id="Specifications">Specifications</h2> <table> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('WebDriver')}}</td> <td>{{Spec2('WebDriver')}}</td> <td>Initial definition</td> </tr> </tbody> </table> <h2 id="Browser_compatibility">Browser compatibility</h2> <p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> <p>{{Compat("webdriver", 2)}}</p> <h2 id="See_also">See also</h2> <ul> <li><a href="/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing">Cross browser testing</a></li> <li><a href="https://seleniumhq.github.io/docs/">Selenium documentation</a> (work in progress)</li> </ul> <p>{{QuickLinksWithSubpages}}</p>