F WebDriver and 环境配置
阅读原文时间:2023年07月11日阅读:1

https://seleniumhq.github.io/docs/wd.html

WEBDRIVER

The biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on a remote machine using the Selenium server, it marks a leap forward in terms of browser automation.

Selenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just WebDriver or sometimes as Selenium 2.

Selenium 1.0 + WebDriver = Selenium 2.0

  • WebDriver is designed in a simpler and more concise programming interface along with addressing some limitations in the Selenium-RC API.
  • WebDriver is a compact object-oriented API when compared to Selenium 1.0.
  • It drives the browser much more effectively and overcomes the limitations of Selenium 1 that affected our functional test coverage, like the file upload or download, pop-ups, and dialogs barrier.
  • WebDriver overcomes the limitation of Selenium RC's single-host origin policy.

DRIVER REQUIREMENTS

Through WebDriver, Selenium supports all major browsers on the market such as Chrom(ium), Firefox, Internet Explorer, Opera, and Safari. Where possible, WebDriver drives the browser using the browser's built-in support for automation, although not all browsers have official support for remote control.

WebDriver's aim is to emulate a real user's interaction with the browser as closely as possible. This is possible at varying levels in different browsers. For more details on the different driver idiosyncracies, please see Driver Idiosyncracies.

Even though all the drivers share a single user-facing interface for contolling the browser, they have slightly different ways of setting up browser sessions. Since many of the driver implementations are provided by third parties, they are not included in the standard Selenium distribution.

Driver instantiation, profile management, and various browser specific settings are examples of parameters that have different requirements depending on the browser. This section explains the basic requirements for getting you started with the different browsers.

Adding Executables to your PATH

Most drivers require an extra executable for Selenium to communicate with the browser. You can manually specify where the executable lives before starting WebDriver, but this can make your tests less portable, as the executables will need to be in the same place on every machine, or included within your test code repository.

By adding a folder containing WebDriver's binaries to your system's path, Selenium will be able to locate the additional binaries without requiring your test code to locate the exact location of the driver.

  • Create a directory to place the executables in, like C:\WebDriver\bin or /opt/WebDriver/bin

  • Add the directory to your PATH:

  • On Windows - Open a command prompt as administrator and the run the following command to permanently add the directory to your path for all users on your machine:

setx /m path "%path%;C:\WebDriver\bin\"

  • Bash users on macOS and Linux - In a terminal:

export PATH=$PATH:/opt/WebDriver/bin >> ~/.profile

  • You are now ready to test your changes. Close all open command prompts and open a new one. Type out the name of one of the binaries in the folder you created in the previous step, e.g:

chromedriver

. If your PATH is configured correctly, you will see some some output relating to the startup of the driver:

Starting ChromeDriver 2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1) on port 9515 Only local connections are allowed.

You can regain control of your command prompt by pressing Ctrl + C.

Quick reference

Browser

Supported OS

Maintained by

Download

Issue Tracker

Chromium/Chrome

Windows

macOS

Linux

Google

Downloads

Issues

Firefox

Windows

macOS

Linux

Mozilla

Downloads

Issues

Edge

Windows 10

Microsoft

Downloads

Issues

Internet Explorer

Windows

Selenium Project

Downloads

Issues

Safari

macOS El Capitan and newer

Apple

Built in

Issues

Opera

Windows

macOS

Linux

Opera

Downloads

Issues

Chromium/Chrome

To drive Chrome or Chromium, you have to download chromedriver and put it in a folder that is on your system's path.

On Linux or macOS, this means modifying the PATH environmental variable. You can see what directories, separated by a colon, make up your system's path by executing the following command:

$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

To include chromedriver on the path if it isn't already, make sure you include the chromedriver binary's parent directory. The following line will set the PATH environmental variable its current content, plus an additional path added after the colon:

$ export PATH="$PATH:/path/to/chromedriver"

When chromedriver is available on your path, you should be able to execute the _chromedriver_ executable from any directory.

To instantiate a Chrome/Chromium session, you can do the following:

WebDriver driver = new ChromeDriver();

Remember that you have to set the path to the chromedriver executable. This is possible using the following line:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

require "selenium-webdriver" driver = Selenium::WebDriver.for :chrome

The chromedriver is implemented as a WebDriver remote server that by exposing Chrome's internal automation proxy interface instructs the browser what to do.

Firefox

Starting with Selenium 3, Mozilla has taken over implementation of Firefox Driver, with geckodriver. The new driver for Firefox is called geckodriver and works with Firefox 48 and newer. Since the Firefox WebDriver is under development, the newer the Firefox version the better the support.

As geckodriver is the new default way of launching Firefox, you can instantiate Firefox in the same way as Selenium 2:

WebDriver driver = new FirefoxDriver();

require "selenium-webdriver" driver = Selenium::WebDriver.for :firefox

If you prefer not to set geckodriver's location using PATH, set the geckodriver binary location programmatically:

System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

It is also possible to set the property at run time:

mvn test -Dwebdriver.gecko.driver=/path/to/geckodriver

It is currently possible to revert to the older, more feature complete Firefox driver, by installing Firefox47.0.1 or 45 ESR and specifying a desired capability of marionette as false. Later releases of Firefox are no longer compatible.

Edge

Edge is Microsoft's newest browser, included with Windows 10 and Server 2016. Updates to Edge are bundled with major Windows updates, so you'll need to download a binary which matches the build number of your currently installed build of Windows. The Edge Developer site contains links to all the available binaries. Bugs against the EdgeDriver implementation can be raised with Microsoft. If you'd like to run tests against Edge, but aren't running Windows 10, Microsoft offer free VMs for testers on the Edge Developer site.

WebDriver driver = new EdgeDriver();

If Edge driver is not present in your path, you can set the path using the following line:

System.setProperty("webdriver.edge.driver", "/path/to/edgedriver");

Internet Explorer

Internet Explorer was Microsoft's default browser until Windows 10, although it is still included in Windows 10. Internet Explorer Driver is the only driver The Selenium project aims to support the same releases Microsoft considers current. Older releases may work, but will be unsupported.

While the Selenium project provides binaries for both the 32-bit and 64-bit versions of Internet Explorer, there are some limitations with Internet Explorer 10 & 11 with the 64-bit driver, but using the 32-bit driver continues to work well. It should be noted that as Internet Explorer preferences are saved against the logged in user's account, some additional setup is required.

WebDriver driver = new InternetExplorerDriver();

If Internet Explorer driver is not present in your path, you can set the path using the following line:

System.setProperty("webdriver.ie.driver", "/path/to/iedriver");

Microsoft also offer a WebDriver binary for Internet Explorer 11 on Windows 7 & 8.1. It has not been updated since 2014 and is based of a draft version of the W3 specification. Jim Evans has an excellent writeup on Microsoft's implementation.

Opera

Current releases of Opera are built on top of the Chromium engine, and WebDriver is now supported via the closed-source Opera Chromium Driver, which can be added to your PATH or as a system property.

Instantiating a driver session is similar to Firefox and Chromium:

WebDriver driver = new OperaDriver();

require "selenium-webdriver" driver = Selenium::WebDriver.for :opera

Safari

Starting with Safari 10 on macOS El Capitan and Sierra, WebDriver support is included with each release of the browser. To enable support:

/usr/bin/safaridriver -p 1337

from the terminal for the first time and type your password at the prompt to authorise WebDriver

You can then start a driver session using:

WebDriver driver = new SafariDriver();

Those looking to automate Safari on iOS should look to the Appium project. Whilst Safari was previously available for Windows, Apple has long since dropped support, making it a poor choice of test platform.

Mock browsers

HtmlUnit

HtmlUnit is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc. It has JavaScript support and is able to work with AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used. It has been moved to a new location.

The source is maintained on svn.

PhantomJS

PhantomJS is a headless browser based on Webkit, albeit a version much older than that used by Google Chrome or Safari. . Whilst historically a popular choice, it would now be wise to avoid PhantomJS. The project has been unmaintained since the 5th of August, so whilst the web will continue to change, PhantomJS will not be updated. This was after Google announced the ability to run Chrome headlessly, something also now offered by Mozilla's Firefox.

BROWSER LAUNCHING AND MANIPULATION

Ruby

Ruby is not installed by default on Windows. Download the latest version and run the installer. You can leave all settings at default values, except at the Installation Destination and Optional Tasks screen check Add Ruby executables to your PATH checkbox. To drive any browser, you have to install selenium-webdriver Ruby gem. To install it, open command prompt and type this:

$ gem install selenium-webdriver

Internet Explorer

Internet Explorer is installed by default on Windows, so no installation is needed. To drive Internet Explorer on Windows, you have to download the latest Internet Explorer Driver and put the file into a folder that is in PATH. To find out which directories are in PATH, type echo %PATH% in command prompt.

$ echo %PATH% C:\Ruby200\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem

C:\Ruby200\bin looks like a good place. Unzip `IEDriverServer` file and move `IEDriverServer.exe` there. This should open a new Internet Explorer window:

System.setProperty("webdriver.ie.driver", /path/to/internetexplorerdriver); WebDriver driver = new InternetExplorerDriver();

require "selenium-webdriver" driver = Selenium::WebDriver.for :internet_explorer

WAITS

WebDriver can generally be said to have a blocking API. Because it is an out-of-process library thatinstructs the browser what to do, and because the web platform has an intrinsically asynchronous nature, WebDriver doesn't track the active, real-time state of the DOM. This comes with some challenges that we will discuss here.

From experience, most intermittents that arise from use of Selenium and WebDriver are connected to race conditions that occur between the browser and the user's instructions. An example could be that the user instructs the browser to navigate to a page, then gets a no such element error when trying to find an element.

Consider the following document:

Race Condition Example<title> <script> var initialised = false; window.addEventListener("load", function() { var newElement = document.createElement("p"); newElement.textContent = "Hello from JavaScript!"; document.body.appendChild(newElement); initialised = true; }); </script></p> <p>The WebDriver instructions might look innocent enough:</p> <p>driver.navigate("file:///race_condition.html") el = driver.find_element_by_tag_name("p") assert el.text == "Hello from JavaScript!"</p> <p>The issue here is that the default <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#page_load_strategy">page load strategy</a> used in WebDriver listens for the document.readyState to change to "complete" before returning from the call to navigate. Because the p element is added after the document has completed loading, this WebDriver script might be intermittent. It “might” be intermittent because no guarantees can be made about elements or events that trigger asynchronously without explicitly waiting—or blocking—on those events.</p> <p>Fortunately, using the normal instruction set available on the <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#webelement">WebElement</a> interface—such as WebElement.click and WebElement.sendKeys—are guaranteed to be synchronous, in that the function calls won't return (or the callback won't trigger in callback-style languages) until the command has been completed in the browser. The advanced user interaction APIs, <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#keyboard">Keyboard</a> and <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#mouse">Mouse</a>, are exceptions as they are explicitly intended as “do what I say” asynchronous commands.</p> <p>Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.</p> <p>To overcome the problem of race conditions between the browser and your WebDriver script, most Selenium clients ship with a wait package. When employing a wait, you are using what is commonly referred to as an <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#explicit_wait">explicit wait</a>.</p> <p>Explicit waiting</p> <p>Explicit waits are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.</p> <p>Since explicit waits allow you to wait for a condition to occur, they make a good fit for synchronising the state between the browser and its DOM, and your WebDriver script.</p> <p>To remedy our buggy instruction set from earlier, we could employ a wait to have the findElement call wait until the dynamically added element from the script has been added to the DOM:</p> <p>from selenium.webdriver.support.ui import WebDriverWait def document_initialised(driver): return driver.execute_script("return initialised") driver.navigate("file:///race_condition.html") WebDriverWait(driver).until(document_initialised) el = driver.find_element_by_tag_name("p") assert el.text == "Hello from JavaScript!"</p> <p>We pass in the condition as a function reference that the wait will run repeatedly until its return value is truthy. A “truthful” return value is anything that evaluates to boolean true in the language at hand, such as a string, number, a boolean, an object (including a WebElement), or a populated (non-empty) sequence or list. That means an empty list evaluates to false. When the condition is truthful and the blocking wait is aborted, the return value from the condition becomes the return value of the wait.</p> <p>With this knowledge, and because the wait utility ignores no such element errors by default, we can refactor our instructions to be more concise:</p> <p>from selenium.webdriver.support.ui import WebDriverWait driver.navigate("file:///race_condition.html") el = WebDriverWait(driver).until(lambda d: return d.find_element_by_tag_name("p")) assert el.text == "Hello from JavaScript!"</p> <p>In that example, we pass in an anonymous function (but we could also define it explicitly as we did earlier so it may be reused). The first and only argument that is passed to our condition is always a reference to our driver object, WebDriver (called d in the example). In a multi-threaded environment, you should be careful to operate on the driver reference passed in to the condition rather than the reference to the driver in the outer scope.</p> <p>Because the wait will swallow no such element errors that are raised when the element isn't found, the condition will retry until the element is found. Then it will take the return value, a WebElement, and pass it back through to our script.</p> <p>If the condition fails, e.g. a truthful return value from the condition is never reached, the wait will throw/raise an error/exception called a timeout error.</p> <p>Options</p> <p>The wait condition can be customised to match your needs. Sometimes it's unnecessary to wait the full extent of the default timeout, as the penalty for not hitting a successful condition can be expensive.</p> <p>The wait lets you pass in an argument to override the timeout:</p> <p>WebDriverWait(driver, timeout=3).until(some_condition)</p> <p>Expected conditions</p> <p>Because it's quite a common occurrence to have to synchronise the DOM and your instructions, most clients also come with a set of predefined expected conditions. As might be obvious by the name, they are conditions that are predefined for frequent wait operations.</p> <p>The conditions available in the different language bindings vary, but this is a non-exhaustive list of a few:</p> <p>alert is present</p> <p>element exists</p> <p>element is visible</p> <p>title contains</p> <p>title is</p> <p>element staleness</p> <p>visible text</p> <p>You can refer to the API documentation for each client binding to find an exhaustive list of expected conditions:</p> <ul> <li>Java's <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html">org.openqa.selenium.support.ui.ExpectedConditions</a> class</li> <li>Python's <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html?highlight=expected">selenium.webdriver.support.expected_conditions</a> class</li> <li>.NET's <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm">OpenQA.Selenium.Support.UI.ExpectedConditions</a> type</li> </ul> <p>Implicit waiting</p> <p>There is a second type of wait that is distinct from <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#explicit_wait">explicit waits</a> called implicit waiting. By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.</p> <p>Implicit waiting for elements to appear is disabled by default and will need to be manually enabled on a per-session basis. Mixing <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#explicit_wait">explicit waits</a> and implicit waiting will cause unintended consequences, namely waits sleeping for the maximum time even if the element is available or condition is true.</p> <p>Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.</p> <p>An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.</p> <p>WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));</p> <p>FluentWait</p> <p>FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.</p> <p>Users may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.</p> <p>// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } });</p> <p>FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA")); fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS); fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); fluentWait.until(new Predicate<By>() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName("TEXTAREA")).sendKeys("text to enter");</p> <p>SUPPORT CLASSES</p> <p>JAVASCRIPT ALERTS, PROMPTS AND CONFIRMATIONS</p> <p>WebDriver provides an API for working with the three types of native popup message offered by JavaScript. These popups are styled by the browser and offer limited customisation.</p> <p>Alerts</p> <p>The simplest of these is referred to as an alert, which shows a custom message, and a single button which dismisses the alert, labelled in most browsers as OK. It can also be dismissed in most browsers by pressing the close button, but this will always do the same thing as the OK button. See an example alert.</p> <p>WebDriver can get the text from the popup and accept or dismiss these alerts.</p> <p>//Click the link to activate the alert driver.findElement(By.linkText("See an example alert")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Store the alert text in a variable String text = alert.getText(); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text("See an example alert").click() # Wait for the alert to be displayed and store it in a variable alert = wait.until(expected_conditions.alert_is_present()) # Store the alert text in a variable text = alert.text # Press the OK button alert.accept()</p> <p>Confirm</p> <p>A confirm box is similar to an alert, except the user can also choose to cancel the message. See a sample confirm.</p> <p>This example also shows a different approach to storing an alert:</p> <p>//Click the link to activate the alert driver.findElement(By.linkText("See a sample confirm")).click(); //Wait for the alert to be displayed wait.until(ExpectedConditions.alertIsPresent()); //Store the alert in a variable Alert alert = driver.switchTo().alert(); //Store the alert in a variable for reuse String text = alert.getText(); //Press the Cancel button alert.dismiss(); # Click the link to activate the alert driver.find_element_by_link_text("See a sample confirm").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = driver.switch_to.alert # Store the alert text in a variable text = alert.text # Press the Cancel button alert.dismiss()</p> <p>Prompt</p> <p>Prompts are similar to confirm boxes, except they also include a text input. Similar to working with form elements, you can use WebDriver's send keys to fill in a response. This will completely replace the placeholder text. Pressing the cancel button will not submit any text. See a sample prompt.</p> <p>//Click the link to activate the alert driver.findElement(By.linkText("See a sample prompt")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Type your message alert.sendKeys("Selenium"); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text("See a sample prompt").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = Alert(driver) # Type your message alert.send_keys("Selenium") # Press the OK button alert.accept()</p> <p>HTTP PROXIES</p> <p>PAGE LOADING STRATEGY</p> <p>WEB ELEMENTS</p> <p>KEYBOARD</p> <p>MOUSE</p> <p>WEBDRIVER</p> <p>Selenium最近的最大变化是包含了WebDriver API。以本地方式作为用户驱动浏览器将在本地或使用Selenium服务器的远程计算机上运行,​​这标志着浏览器自动化方面的一次飞跃。</p> <p>Selenium WebDriver与RC的作用相同,并且包含原始的1.x绑定。它涉及语言绑定和各个浏览器控制代码的实现。这通常被称为WebDriver 或有时被称为Selenium 2。</p> <p>Selenium 1.0 + WebDriver = Selenium 2.0</p> <ul> <li>WebDriver的设计更简单,更简洁的编程界面,同时解决了Selenium-RC API中的一些限制。</li> <li>与Selenium 1.0相比,WebDriver是一个紧凑的面向对象API。</li> <li>它更有效地驱动浏览器,并克服了Selenium 1的局限性,这些限制影响了我们的功能测试覆盖范围,如文件上传或下载,弹出窗口和对话框屏障。</li> <li>WebDriver克服了Selenium RC的<a rel="nofollow noopener noreferrer" href="https://en.wikipedia.org/wiki/Same-origin_policy">单主机起源策略</a>的局限性 <a rel="nofollow noopener noreferrer" href="https://en.wikipedia.org/wiki/Same-origin_policy">。</a></li> </ul> <p>驱动要求</p> <p>通过WebDriver,Selenium支持市场上所有主流浏览器,如Chrom(ium),Firefox,Internet Explorer,Opera和Safari。在可能的情况下,WebDriver使用浏览器对自动化的内置支持来驱动浏览器,但并非所有浏览器都支持远程控制。</p> <p>WebDriver的目标是尽可能模仿真实用户与浏览器的交互。在不同的浏览器中,这是可能的。有关不同驱动程序特性的更多详细信息,请参阅<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/drivers.html">驱动</a>程序特性。</p> <p>即使所有驱动程序共享一个用于控制浏览器的面向用户的界面,但它们在设置浏览器会话方面略有不同。由于许多驱动程序实现由第三方提供,因此它们不包含在标准的Selenium发行版中。</p> <p>驱动程序实例化,配置文件管理以及各种浏览器特定设置是根据浏览器具有不同要求的参数示例。本节介绍让您开始使用不同浏览器的基本要求。</p> <p>将可执行文件添加到您的PATH</p> <p>大多数驱动程序需要一个额外的可执行文件让Selenium与浏览器通信。您可以在启动WebDriver之前手动指定可执行文件的位置,但这可能会使测试的可移植性降低,因为可执行文件需要位于每台计算机的相同位置,或者包含在测试代码库中。</p> <p>通过将包含WebDriver二进制文件的文件夹添加到系统路径中,Selenium将能够找到其他二进制文件,而无需您的测试代码来查找驱动程序的确切位置。</p> <ul> <li><p>创建一个目录来放置浏览器驱动可执行文件,例如 C:\ WebDriver \ bin或/ opt / WebDriver / bin</p></li> <li><p>将该目录添加到系统环境变量PATH中:</p></li> <li><p>在Windows上 - 以管理员身份打开命令提示符,然后运行以下命令将该目录永久地添加到您计算机上所有用户的路径中:</p></li> </ul> <p>setx /m path “%path%;C:\WebDriver \bin\”</p> <ul> <li>在macOS和Linux上使用bash用户 - 在终端中:</li> </ul> <p>export PATH=$PATH:/opt/WebDriver/bin >> 〜/.profile</p> <ul> <li>您现在可以验证您的更改是否有效。关闭所有打开的命令提示符并打开一个新提示符。在上一步创建的文件夹中输入其中一个二进制文件的名称,例如:</li> </ul> <p>chromedriver</p> <p>。如果您的PATH配置正确,您会看到一些与驱动程序启动有关的输出:</p> <p>Starting ChromeDriver 2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1) on port 9515 Only local connections are allowed.</p> <p>按下,您可以重新获得对命令提示符的控制Ctrl + C。</p> <p>快速参考</p> <p>浏览器</p> <p>支持的OS</p> <p>由…维护</p> <p>下载</p> <p>问题跟踪器</p> <p>铬/铬</p> <p>Windows </p> <p>macOS </p> <p>Linux</p> <p>谷歌</p> <p><a rel="nofollow noopener noreferrer" href="https://chromedriver.storage.googleapis.com/index.html">下载</a></p> <p><a rel="nofollow noopener noreferrer" href="https://bugs.chromium.org/p/chromedriver/issues/list">问题</a></p> <p>火狐</p> <p>Windows </p> <p>macOS </p> <p>Linux</p> <p>Mozilla</p> <p><a rel="nofollow noopener noreferrer" href="https://github.com/mozilla/geckodriver/releases">下载</a></p> <p><a rel="nofollow noopener noreferrer" href="https://github.com/mozilla/geckodriver/issues">问题</a></p> <p>边缘</p> <p>Windows 10</p> <p>微软</p> <p><a rel="nofollow noopener noreferrer" href="https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/">下载</a></p> <p><a rel="nofollow noopener noreferrer" href="https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/?page=1&q=webdriver">问题</a></p> <p>IE浏览器</p> <p>视窗</p> <p>硒项目</p> <p><a rel="nofollow noopener noreferrer" href="https://selenium-release.storage.googleapis.com/index.html">下载</a></p> <p><a rel="nofollow noopener noreferrer" href="https://github.com/SeleniumHQ/selenium/labels/D-IE">问题</a></p> <p>苹果浏览器</p> <p>macOS El Capitan和更新的</p> <p>苹果</p> <p>内置</p> <p><a rel="nofollow noopener noreferrer" href="https://bugreport.apple.com/logon">问题</a></p> <p>歌剧</p> <p>Windows </p> <p>macOS </p> <p>Linux</p> <p>歌剧</p> <p><a rel="nofollow noopener noreferrer" href="https://github.com/operasoftware/operachromiumdriver/releases">下载</a></p> <p><a rel="nofollow noopener noreferrer" href="https://github.com/operasoftware/operachromiumdriver/issues">问题</a></p> <p>铬/铬</p> <p>要驱动Chrome或Chromium,您必须下载 <a rel="nofollow noopener noreferrer" href="https://sites.google.com/a/chromium.org/chromedriver/downloads">chromedriver</a> 并将其放入系统路径中的文件夹。</p> <p>在Linux或macOS上,这意味着修改PATH环境变量。您可以通过执行以下命令来查看以冒号分隔的目录是否构成系统的路径:</p> <p>$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</p> <p>如果路径中尚未包含chromedriver,请确保包含chromedriver二进制文件的父目录。以下行将设置PATH环境变量的当前内容,并在冒号后添加一个额外的路径:</p> <p>$ export PATH="$PATH:/path/to/chromedriver"</p> <p>当您的路径上有可用的chromedriver时,您应该能够从任何目录执行_chromedriver_可执行文件。</p> <p>要实例化Chrome / Chromium会话,您可以执行以下操作:</p> <p>WebDriver driver = new ChromeDriver();</p> <p>请记住,您必须将路径设置为chromedriver可执行文件。这可以使用以下行:</p> <p>System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");</p> <p>require "selenium-webdriver" driver = Selenium::WebDriver.for :chrome</p> <p>chromedriver被当作WebDriver远程服务器,它通过公开chrome的内部自动代理接口来指挥浏览器做什么</p> <p>火狐</p> <p>从Selenium 3开始,Mozilla利用<a rel="nofollow noopener noreferrer" href="https://github.com/mozilla/geckodriver">geckodriver</a>接管了Firefox Driver的实现。Firefox的新驱动称为geckodriver,适用于Firefox 48及更新版本。由于Firefox WebDriver正在开发中,Firefox版本越新越好。</p> <p>由于geckodriver是启动Firefox的新默认方式,因此您可以像Selenium 2一样实例化Firefox:</p> <p>WebDriver driver = new FirefoxDriver();</p> <p>require "selenium-webdriver" driver = Selenium::WebDriver.for :firefox</p> <p>如果您不想使用PATH设置geckodriver的位置,请以编程方式设置geckodriver二进制位置:</p> <p>System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");</p> <p>也可以在运行时设置属性:</p> <p>mvn test -Dwebdriver.gecko.driver=/path/to/geckodriver</p> <p>目前有可能通过安装Firefox <a rel="nofollow noopener noreferrer" href="https://ftp.mozilla.org/pub/firefox/releases/47.0.1/">47.0.1</a> 或<a rel="nofollow noopener noreferrer" href="https://ftp.mozilla.org/pub/firefox/releases/45.0esr/">45 ESR</a> 并将所需的木偶功能指定为 false来恢复到较老的,功能更完整的Firefox驱动程序 。Firefox的后续版本不再兼容。</p> <p>Edge</p> <p>Edge是Microsoft最新的浏览器,包含在Windows 10和Server 2016中。Edge的更新与主要Windows更新捆绑在一起,因此您需要下载与您当前安装的Windows版本号相匹配的二进制文件。该<a rel="nofollow noopener noreferrer" href="https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/">边缘开发人员网站</a> 包含指向所有可用的二进制文件。<a rel="nofollow noopener noreferrer" href="https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/?page=1&q=webdriver">微软</a>可以提出针对EdgeDriver实施的错误 。如果您希望针对Edge运行测试,但不运行Windows 10,Microsoft会在<a rel="nofollow noopener noreferrer" href="https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/">Edge开发人员网站</a>上为测试人员提供免费虚拟机。</p> <p>WebDriver driver = new EdgeDriver();</p> <p>如果Edge驱动程序不在您的路径中,则可以使用以下行设置路径:</p> <p>System.setProperty("webdriver.edge.driver", "/path/to/edgedriver");</p> <p>IE浏览器</p> <p>在Windows 10之前,Internet Explorer是微软的默认浏览器,尽管它仍然包含在Windows 10中。Internet Explorer驱动程序是唯一的驱动程序Selenium项目旨在支持<a rel="nofollow noopener noreferrer" href="https://support.microsoft.com/en-gb/help/17454/lifecycle-support-policy-faq-internet-explorer">Microsoft认为的最新</a>版本 。旧版本可能有效,但不受支持。</p> <p>虽然Selenium项目为32位和64位版本的Internet Explorer提供了二进制文件,但Internet Explorer 10和11在64位驱动程序方面存在一些<a rel="nofollow noopener noreferrer" href="http://jimevansmusic.blogspot.co.uk/2014/09/screenshots-sendkeys-and-sixty-four.html">局限性</a>,但使用32位驱动程序仍能正常运行。应该注意的是,由于Internet Explorer首选项是针对登录用户的帐户保存的,<a rel="nofollow noopener noreferrer" href="https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration">因此需要进行</a>一些<a rel="nofollow noopener noreferrer" href="https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration">额外的设置</a>。</p> <p>WebDriver driver = new InternetExplorerDriver();</p> <p>如果Internet Explorer驱动程序不在您的路径中,则可以使用以下行来设置路径:</p> <p>System.setProperty("webdriver.ie.driver", "/path/to/iedriver");</p> <p>微软还为<a rel="nofollow noopener noreferrer" href="https://www.microsoft.com/en-gb/download/details.aspx?id=44069">Windows 7和8.1上的Internet Explorer 11</a>提供WebDriver二进制文件。它自2014年以来一直未更新,并基于W3规范的草案版本。<a rel="nofollow noopener noreferrer" href="http://jimevansmusic.blogspot.co.uk/2014/09/using-internet-explorer-webdriver.html">Jim Evans</a>对微软的实施有很好的评论。</p> <p>Opera</p> <p>Opera的当前版本建立在Chromium引擎之上,WebDriver现在通过闭源 <a rel="nofollow noopener noreferrer" href="https://github.com/operasoftware/operachromiumdriver/releases">Opera Chromium驱动程序提供支持</a>,该<a rel="nofollow noopener noreferrer" href="https://github.com/operasoftware/operachromiumdriver/releases">驱动程序</a>可以<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#adding_executables_to_your_path">添加到PATH中</a> 或作为系统属性。</p> <p>实例化驱动程序会话与Firefox和Chromium类似:</p> <p>WebDriver driver = new OperaDriver();</p> <p>require "selenium-webdriver" driver = Selenium::WebDriver.for :opera</p> <p>Safari</p> <p>从macOS El Capitan和Sierra上的Safari 10开始,每个浏览器版本都包含WebDriver支持。启用支持:</p> <p>/ usr / bin / safaridriver -p 1337</p> <p>从终端第一次,并在提示输入您的密码授权WebDriver</p> <p>然后可以使用以下命令启动驱动程序会话</p> <p>WebDriver driver = new SafariDriver();</p> <p>那些希望在iOS上自动化Safari的人应该看看 <a rel="nofollow noopener noreferrer" href="http://appium.io/">Appium项目</a>。虽然以前Safari可用于Windows,但Apple早已不再维护,使其成为测试平台的糟糕选择。</p> <p>模拟浏览器</p> <p>HtmlUnit</p> <p>HtmlUnit是一个“Java程序的无GUI浏览器”。它模拟HTML文档,并提供一个API,使您可以调用页面,填写表单,单击链接等。它支持JavaScript,可以使用AJAX库,根据所用配置模拟Chrome,Firefox或Internet Explorer。它已被移到一个 <a rel="nofollow noopener noreferrer" href="http://htmlunit.sourceforge.net/gettingStarted.html">新的</a>位置。</p> <p>源代码保存在svn上。</p> <p>PhantomJS</p> <p>PhantomJS是一款基于Webkit的无头浏览器,尽管它的版本比Google Chrome或Safari使用的版本要早得多。。虽然历史上流行的选择,但避免PhantomJS现在是明智的。该项目<a rel="nofollow noopener noreferrer" href="https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE">自8月5日以来</a>一直未得到维护 ,因此虽然网络将继续发生变化,但PhantomJS不会更新。这是在谷歌宣布无线运行Chrome的能力之后,现在Mozilla的Firefox也提供了一些功能。</p> <p>浏览器启动和操作</p> <p>Ruby</p> <p>在Windows上默认不安装Ruby。下载最新<a rel="nofollow noopener noreferrer" href="http://rubyinstaller.org/downloads">版本</a>并运行安装程序。您可以将所有设置保留为默认值,除了在“ 安装目标”和“可选任务”屏幕中检查将Ruby可执行文件添加到PATH复选框。要驱动任何浏览器,您必须安装selenium-webdriver Ruby gem。要安装它,请打开命令提示符并输入:</p> <p>$ gem install selenium-webdriver</p> <p>IE浏览器</p> <p>Internet Explorer默认安装在Windows上,因此不需要安装。要在Windows上驱动Internet Explorer,必须下载最新的<a rel="nofollow noopener noreferrer" href="http://www.seleniumhq.org/download/">Internet Explorer驱动程序</a>,并将该文件放入PATH中的文件夹中。要找出PATH中的哪些目录,请在命令提示符下键入echo%PATH%。</p> <p>$ echo %PATH% C:\Ruby200\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem</p> <p>C:\ Ruby200 \ bin看起来像个好地方。解压缩`IEDriverServer`文件并将`IEDriverServer.exe`移动到那里。这应该打开一个新的Internet Explorer窗口:</p> <p>System.setProperty("webdriver.ie.driver", /path/to/internetexplorerdriver); WebDriver driver = new InternetExplorerDriver();</p> <p>require "selenium-webdriver" driver = Selenium::WebDriver.for :internet_explorer</p> <p>WAITS功能</p> <p>通常可以说WebDriver具有阻塞API。由于它是一个进程外库,它 指示浏览器执行什么操作,并且因为Web平台具有内在的异步特性,所以WebDriver不会跟踪DOM的活动实时状态。这带来了一些挑战,我们将在这里讨论。。</p> <p>根据经验,使用Selenium和WebDriver产生的大部分间歇性事件都与浏览器和用户指令之间出现的争用条件相关联。一个例子可能是用户指示浏览器导航到页面,然后在尝试查找元素时没有发现此类元素错误。</p> <p>参考以下文件:</p> <p><!doctype html> <meta charset=utf-8> <title>Race Condition Example<title> <script> var initialised = false; window.addEventListener("load", function() { var newElement = document.createElement("p"); newElement.textContent = "Hello from JavaScript!"; document.body.appendChild(newElement); initialised = true; }); </script></p> <p>WebDriver指令可能看起来无辜:</p> <p>driver.navigate("file:///race_condition.html") el = driver.find_element_by_tag_name("p") assert el.text == "Hello from JavaScript!"</p> <p>这里的问题是WebDriver中使用的默认页面加载策略监听文档。readyState更改为“complete”,然后从调用返回导航。因为p元素是在文档完成加载之后添加的,所以这个WebDriver脚本可能是间歇性的。它“可能”是间歇性的,因为不能对异步触发的元素或事件作出保证,而不需要显式地等待或阻塞这些事件。</p> <p>幸运的是,使用<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#webelement">WebElement</a>接口上可用的普通指令集(例如WebElement.click和WebElement.sendKeys)保证是同步的,因为函数调用不会返回(或者回调将不会以回调式触发语言),直到浏览器中的命令完成。高级用户交互API <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#keyboard">键盘</a>和 <a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#mouse">鼠标</a>是例外,因为它们显式地用作“按我说的做”异步命令。</p> <p>在继续下一步之前,等待会让自动任务执行过去一段时间。</p> <p>为了克服浏览器和WebDriver脚本之间的竞争状况问题,大多数Selenium客户端都附带一个等待包。当采用等待时,您正在使用通常被称为<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#explicit_wait">显式等待的内容</a>。</p> <p>显式等待</p> <p>Selenium客户端可以使用显式等待来使用命令式的过程语言。它们允许您的代码停止程序执行,或者冻结线程,直到您通过的条件得到解决。以特定频率调用条件,直到等待超时。这意味着只要条件返回一个虚假值,它就会继续尝试和等待。</p> <p>由于显式等待允许您等待条件发生,因此它们非常适合于同步浏览器及其DOM和WebDriver脚本之间的状态。</p> <p>为了补救我们早期的错误指令集,我们可以等待findElement调用等待,直到脚本中动态添加的元素被添加到DOM:</p> <p>from selenium.webdriver.support.ui import WebDriverWait def document_initialised(driver): return driver.execute_script("return initialised") driver.navigate("file:///race_condition.html") WebDriverWait(driver).until(document_initialised) el = driver.find_element_by_tag_name("p") assert el.text == "Hello from JavaScript!"</p> <p>我们将条件作为函数引用传递,等待将重复运行,直到其返回值为真。一个“真实的”返回值是任何在手头语言中评估为boolean true的东西,如字符串,数字,布尔值,对象(包括WebElement)或填充(非空)序列或列表。这意味着一个空列表的计算结果为false。当条件是真实的并且阻塞等待被中止时,来自条件的返回值变成等待的返回值。</p> <p>有了这些知识,并且由于等待实用程序默认情况下不会忽略此类元素错误,所以我们可以将我们的指示重构得更加简洁:</p> <p>from selenium.webdriver.support.ui import WebDriverWait driver.navigate("file:///race_condition.html") el = WebDriverWait(driver).until(lambda d: return d.find_element_by_tag_name("p")) assert el.text == "Hello from JavaScript!"</p> <p>在这个例子中,我们传入一个匿名函数(但我们也可以像前面那样明确地定义它,以便它可以被重用)。传递给我们的条件的第一个也是唯一的参数始终是对我们的驱动程序对象WebDriver (在该示例中称为d)的引用。在多线程环境中,您应该小心地对传入条件的驱动程序引用进行操作,而不是在外部范围中对驱动程序的引用。</p> <p>因为等待将不会消除元素未找到时引发的此类元素错误,则条件将重试,直到找到该元素。然后它将获得返回值,一个WebElement,并将其传回给我们的脚本。</p> <p>如果条件失败,例如从条件得到的真实返回值永远不会达到,那么等待将抛出/引发称为超时错误的错误/异常。</p> <p>选项</p> <p>等待条件可以定制以满足您的需求。有时没有必要等待默认超时的全部范围,因为没有达到成功条件的代价会很昂贵。</p> <p>等待让你传入一个参数来覆盖超时:</p> <p>WebDriverWait(driver, timeout=3).until(some_condition)</p> <p>预期条件</p> <p>因为必须同步DOM和您的指令是很常见的事情,所以大多数客户还会附带一组预定义的预期条件。由名称可能很明显,它们是为频繁等待操作而预定义的条件。</p> <p>不同语言绑定中可用的条件各不相同,但这是一个非穷举的列表:</p> <p>警报存在</p> <p>元素存在</p> <p>元素是可见的</p> <p>标题包含</p> <p>标题是</p> <p>元素陈旧</p> <p>可见的文本</p> <p>您可以参考每个客户端绑定的API文档以查找预期条件的详尽列表:</p> <ul> <li>Java的<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html">org.openqa.selenium.support.ui.ExpectedConditions</a>类</li> <li>Python的<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html?highlight=expected">selenium.webdriver.support.expected_conditions</a>类</li> <li>.NET的<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm">OpenQA.Selenium.Support.UI.ExpectedConditions</a>类型</li> </ul> <p>隐含的等待</p> <p>还有第二种类型的等待与<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#explicit_wait">显式等待</a>不同, 称为隐式等待。通过隐式等待,WebDriver在尝试查找任何元素时轮询DOM一段时间。当网页上的某些元素不可立即使用并需要一些时间加载时,这可能很有用。</p> <p>默认情况下隐式等待元素出现,并且需要在每个会话的基础上手动启用。混合<a rel="nofollow noopener noreferrer" href="https://seleniumhq.github.io/docs/wd.html#explicit_wait">明确的等待</a>和隐含的等待将导致意想不到的后果,即等待最大时间睡眠,即使元素可用或条件为真。</p> <p>警告: 不要混合隐式和显式等待。这样做会导致无法预测的等待时间。例如,设置10秒的隐式等待和15秒的明确等待可能会导致20秒后发生超时。</p> <p>隐含的等待是告诉WebDriver在尝试查找一个或多个元素(如果它们不是立即可用的)时轮询DOM一段时间。默认设置为0,意味着禁用。一旦设置,隐含的等待就设置为会话的整个生命周期。</p> <p>WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));</p> <p>FluentWait</p> <p>FluentWait实例定义了等待条件的最长时间以及检查条件的频率。</p> <p>用户可以配置等待,以在等待时忽略特定类型的异常,例如NoSuchElementException在页面上搜索元素时。</p> <p>// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } });</p> <p>FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA")); fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS); fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); fluentWait.until(new Predicate<By>() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName("TEXTAREA")).sendKeys("text to enter");</p> <p>支持类</p> <p>JAVASCRIPT警报,提示和确认</p> <p>WebDriver提供了一个用于处理JavaScript提供的三种本地弹出消息的API。这些弹出窗口由浏览器设置并提供有限的自定义。</p> <p>Alerts</p> <p>其中最简单的就是一个警报,它显示了一个自定义消息,以及一个按钮,它解除了警报,在大多数浏览器中标记为OK。它也可以通过按下关闭按钮在大多数浏览器中被解散,但是这总是和OK按钮一样。查看示例警报。</p> <p>WebDriver可以从弹出窗口中获取文本并接受或拒绝这些警报。</p> <p>//Click the link to activate the alert driver.findElement(By.linkText("See an example alert")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Store the alert text in a variable String text = alert.getText(); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text("See an example alert").click() # Wait for the alert to be displayed and store it in a variable alert = wait.until(expected_conditions.alert_is_present()) # Store the alert text in a variable text = alert.text # Press the OK button alert.accept()</p> <p>Confirm</p> <p>确认框与提示类似,除了用户也可以选择取消消息。查看样本确认。</p> <p>此示例还显示了一种用于存储警报的不同方法:</p> <p>//Click the link to activate the alert driver.findElement(By.linkText("See a sample confirm")).click(); //Wait for the alert to be displayed wait.until(ExpectedConditions.alertIsPresent()); //Store the alert in a variable Alert alert = driver.switchTo().alert(); //Store the alert in a variable for reuse String text = alert.getText(); //Press the Cancel button alert.dismiss(); # Click the link to activate the alert driver.find_element_by_link_text("See a sample confirm").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = driver.switch_to.alert # Store the alert text in a variable text = alert.text # Press the Cancel button alert.dismiss()</p> <p>Prompt</p> <p>提示与确认框相似,除了它们还包含文本输入。与使用表单元素类似,您可以使用WebDriver的发送键来填写响应。这将完全取代占位符文本。按取消按钮不会提交任何文本。 查看示例提示。</p> <p>//Click the link to activate the alert driver.findElement(By.linkText("See a sample prompt")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Type your message alert.sendKeys("Selenium"); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text("See a sample prompt").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = Alert(driver) # Type your message alert.send_keys("Selenium") # Press the OK button alert.accept()</p> <p>HTTP代理</p> <p>页面加载策略</p> <p>WEB元素</p> <p>键盘</p> <p>鼠标</p></div></div><div class="MuiGrid-root jss8 MuiGrid-item MuiGrid-grid-xs-true MuiGrid-grid-md-3"><div class="MuiTypography-root jss26 MuiTypography-body1"><div class="MuiTypography-root jss27 MuiTypography-body1"><canvas style="height:108px;width:108px" height="108" width="108"></canvas><div class="MuiTypography-root jss28 MuiTypography-body1"><p class="MuiTypography-root jss29 MuiTypography-body1">手机扫一扫</p><p class="MuiTypography-root jss29 MuiTypography-body1">移动阅读更方便</p></div></div></div><div class="MuiTypography-root jss9 MuiTypography-body1"><div class="MuiTypography-root jss30 MuiTypography-body1" style="height:150px"><div class="swiper-container jss32"><div class="swiper-pagination"></div><div class="swiper-wrapper"><div class="swiper-slide jss32"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss32 MuiTypography-colorInherit" target="_blank" rel="nofollow noopener noreferrer" href="https://qd.rs/aliyun"><img alt="阿里云服务器" class="jss31" src="https://article.cdnof.com/promotion/aliyun.jpg"/></a></div><div class="swiper-slide jss32"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss32 MuiTypography-colorInherit" target="_blank" rel="nofollow noopener noreferrer" href="https://qd.rs/tencent"><img alt="腾讯云服务器" class="jss31" src="https://article.cdnof.com/promotion/tencent.jpg"/></a></div><div class="swiper-slide jss32"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss32 MuiTypography-colorInherit" target="_blank" rel="nofollow noopener noreferrer" href="https://qd.rs/qiniu"><img alt="七牛云服务器" class="jss31" src="https://article.cdnof.com/promotion/qiniu.png"/></a></div></div></div></div></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiTypography-root jss33 MuiTypography-body1"><p class="MuiTypography-root jss34 MuiTypography-body1">你可能感兴趣的文章</p><div class="MuiList-root MuiList-padding" aria-label="main mailbox folders"><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><svg class="MuiSvgIcon-root MuiSvgIcon-colorError" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-5 14h-2V9h-2V7h4v10z"></path></svg></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/2f80d1a6-b248-4bbe-bee3-f342411680b4">selenium+python处理Alert弹窗</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4 8c0 1.11-.9 2-2 2h-2v2h4v2H9v-4c0-1.11.9-2 2-2h2V9H9V7h4c1.1 0 2 .89 2 2v2z"></path></svg></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/080b1cbb-ae2f-4602-a55d-6b6371986b5a">Selenium_使用switch_to.alert处理弹窗(14)</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><svg class="MuiSvgIcon-root MuiSvgIcon-colorSecondary" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19.01 3h-14c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4 7.5c0 .83-.67 1.5-1.5 1.5.83 0 1.5.67 1.5 1.5V15c0 1.11-.9 2-2 2h-4v-2h4v-2h-2v-2h2V9h-4V7h4c1.1 0 2 .89 2 2v1.5z"></path></svg></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/f6d70ae9-0d23-41fe-8979-f9a2441037a5">Python3+Selenium3自动化测试-(六)</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">4</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/534eb274-138c-47f3-bc2b-238c90c5e84c">Selenium4+Python3系列(十) - Page Object设计模式</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">5</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/a6d3011e-6fe5-45cd-a4c8-d625f816964f">selenium------关于switch_to的用法场景</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">6</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/976f7694-c519-4463-9df0-89eb8b18d418">Selenium和PhantomJS</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">7</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/8093b371-20a2-4a56-baaa-a99c5037e154">C#爬虫(03):使用Selenium</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">8</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/3f0d13e8-565e-45b2-96ad-8b42524b5a9f">python selenium表单定位</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">9</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/ca83fcfb-03f4-4908-85b3-1dba13889498">Python-selenium显示等待</a></div><hr class="MuiDivider-root"/></div><div class="MuiTypography-root MuiTypography-body1"><div class="MuiButtonBase-root MuiListItem-root jss37 MuiListItem-gutters MuiListItem-button" tabindex="0" role="button" aria-disabled="false"><div class="MuiListItemIcon-root jss36"><p class="MuiTypography-root MuiTypography-body1">10</p></div><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss35 MuiTypography-colorInherit" target="_blank" href="https://v2as.com/article/8c9f5349-7cb2-4b2f-8dae-6e6023f65e6d">虫师Selenium2+Python_6、Selenium IDE</a></div></div></div></div></div></div></div></div><footer style="margin-top:30px"><p class="MuiTypography-root MuiTypography-body2 MuiTypography-colorTextSecondary MuiTypography-alignCenter">Copyright © <a class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-colorInherit" href="https://v2as.com" title="哇哦,有大量工具等你探索">V2AS | 问路</a> <!-- -->2024<!-- --> <!-- -->.</p><p class="MuiTypography-root MuiTypography-body2 MuiTypography-colorTextSecondary MuiTypography-alignCenter"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-colorInherit" rel="nofollow noopener noreferrer" href="https://beian.miit.gov.cn/">浙ICP备15029886号</a></p></footer></div></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"article":{"article_id":"22721bca-b928-4959-add5-a2042074a7fe","title":"F WebDriver and 环境配置","link":"","description":"https://seleniumhq.github.io/docs/wd.html\nWEBDRIVER\nThe biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on","image":"","keywords":["WebDriver","driver","alert","Selenium","wait","浏览器","text","PATH","Windows","element"],"created_at":"2023-07-11T05:21:48.227Z","html":"\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html\"\u003ehttps://seleniumhq.github.io/docs/wd.html\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eWEBDRIVER\u003c/p\u003e\n\u003cp\u003eThe biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on a remote machine using the Selenium server, it marks a leap forward in terms of browser automation.\u003c/p\u003e\n\u003cp\u003eSelenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just\u0026nbsp;WebDriver\u0026nbsp;or sometimes as\u0026nbsp;Selenium 2.\u003c/p\u003e\n\u003cp\u003eSelenium 1.0 + WebDriver = Selenium 2.0\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eWebDriver is designed in a simpler and more concise programming interface along with addressing some limitations in the Selenium-RC API.\u003c/li\u003e\n\u003cli\u003eWebDriver is a compact object-oriented API when compared to Selenium 1.0.\u003c/li\u003e\n\u003cli\u003eIt drives the browser much more effectively and overcomes the limitations of Selenium 1 that affected our functional test coverage, like the file upload or download, pop-ups, and dialogs barrier.\u003c/li\u003e\n\u003cli\u003eWebDriver overcomes the limitation of Selenium RC's\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://en.wikipedia.org/wiki/Same-origin_policy\"\u003esingle-host origin policy.\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eDRIVER REQUIREMENTS\u003c/p\u003e\n\u003cp\u003eThrough WebDriver, Selenium supports all major browsers on the market such as Chrom(ium), Firefox, Internet Explorer, Opera, and Safari. Where possible, WebDriver drives the browser using the browser's built-in support for automation, although not all browsers have official support for remote control.\u003c/p\u003e\n\u003cp\u003eWebDriver's aim is to emulate a real user's interaction with the browser as closely as possible. This is possible at varying levels in different browsers. For more details on the different driver idiosyncracies, please see\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/drivers.html\"\u003eDriver Idiosyncracies\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eEven though all the drivers share a single user-facing interface for contolling the browser, they have slightly different ways of setting up browser sessions. Since many of the driver implementations are provided by third parties, they are not included in the standard Selenium distribution.\u003c/p\u003e\n\u003cp\u003eDriver instantiation, profile management, and various browser specific settings are examples of parameters that have different requirements depending on the browser. This section explains the basic requirements for getting you started with the different browsers.\u003c/p\u003e\n\u003cp\u003eAdding Executables to your PATH\u003c/p\u003e\n\u003cp\u003eMost drivers require an extra executable for Selenium to communicate with the browser. You can manually specify where the executable lives before starting WebDriver, but this can make your tests less portable, as the executables will need to be in the same place on every machine, or included within your test code repository.\u003c/p\u003e\n\u003cp\u003eBy adding a folder containing WebDriver's binaries to your system's path, Selenium will be able to locate the additional binaries without requiring your test code to locate the exact location of the driver.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003cp\u003eCreate a directory to place the executables in, like\u0026nbsp;C:\\WebDriver\\bin\u0026nbsp;or\u0026nbsp;/opt/WebDriver/bin\u003c/p\u003e\u003c/li\u003e\n\u003cli\u003e\u003cp\u003eAdd the directory to your PATH:\u003c/p\u003e\u003c/li\u003e\n\u003cli\u003e\u003cp\u003eOn Windows - Open a command prompt as administrator and the run the following command to permanently add the directory to your path for all users on your machine:\u003c/p\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003esetx /m path \"%path%;C:\\WebDriver\\bin\\\"\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eBash users on macOS and Linux - In a terminal:\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eexport PATH=$PATH:/opt/WebDriver/bin \u0026gt;\u0026gt; ~/.profile\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eYou are now ready to test your changes. Close all open command prompts and open a new one. Type out the name of one of the binaries in the folder you created in the previous step, e.g:\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003echromedriver\u003c/p\u003e\n\u003cp\u003e. If your\u0026nbsp;PATH\u0026nbsp;is configured correctly, you will see some some output relating to the startup of the driver:\u003c/p\u003e\n\u003cp\u003eStarting ChromeDriver 2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1) on port 9515 Only local connections are allowed.\u003c/p\u003e\n\u003cp\u003eYou can regain control of your command prompt by pressing\u0026nbsp;Ctrl + C.\u003c/p\u003e\n\u003cp\u003eQuick reference\u003c/p\u003e\n\u003cp\u003eBrowser\u003c/p\u003e\n\u003cp\u003eSupported OS\u003c/p\u003e\n\u003cp\u003eMaintained by\u003c/p\u003e\n\u003cp\u003eDownload\u003c/p\u003e\n\u003cp\u003eIssue Tracker\u003c/p\u003e\n\u003cp\u003eChromium/Chrome\u003c/p\u003e\n\u003cp\u003eWindows\u003c/p\u003e\n\u003cp\u003emacOS\u003c/p\u003e\n\u003cp\u003eLinux\u003c/p\u003e\n\u003cp\u003eGoogle\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://chromedriver.storage.googleapis.com/index.html\"\u003eDownloads\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://bugs.chromium.org/p/chromedriver/issues/list\"\u003eIssues\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eFirefox\u003c/p\u003e\n\u003cp\u003eWindows\u003c/p\u003e\n\u003cp\u003emacOS\u003c/p\u003e\n\u003cp\u003eLinux\u003c/p\u003e\n\u003cp\u003eMozilla\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/mozilla/geckodriver/releases\"\u003eDownloads\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/mozilla/geckodriver/issues\"\u003eIssues\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eEdge\u003c/p\u003e\n\u003cp\u003eWindows 10\u003c/p\u003e\n\u003cp\u003eMicrosoft\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/\"\u003eDownloads\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/?page=1\u0026q=webdriver\"\u003eIssues\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eInternet Explorer\u003c/p\u003e\n\u003cp\u003eWindows\u003c/p\u003e\n\u003cp\u003eSelenium Project\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://selenium-release.storage.googleapis.com/index.html\"\u003eDownloads\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/SeleniumHQ/selenium/labels/D-IE\"\u003eIssues\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eSafari\u003c/p\u003e\n\u003cp\u003emacOS El Capitan and newer\u003c/p\u003e\n\u003cp\u003eApple\u003c/p\u003e\n\u003cp\u003eBuilt in\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://bugreport.apple.com/logon\"\u003eIssues\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eOpera\u003c/p\u003e\n\u003cp\u003eWindows\u003c/p\u003e\n\u003cp\u003emacOS\u003c/p\u003e\n\u003cp\u003eLinux\u003c/p\u003e\n\u003cp\u003eOpera\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/releases\"\u003eDownloads\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/issues\"\u003eIssues\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eChromium/Chrome\u003c/p\u003e\n\u003cp\u003eTo drive Chrome or Chromium, you have to download\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://sites.google.com/a/chromium.org/chromedriver/downloads\"\u003echromedriver\u003c/a\u003e\u0026nbsp;and put it in a folder that is on your system's path.\u003c/p\u003e\n\u003cp\u003eOn Linux or macOS, this means modifying the\u0026nbsp;PATH\u0026nbsp;environmental variable. You can see what directories, separated by a colon, make up your system's path by executing the following command:\u003c/p\u003e\n\u003cp\u003e$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u003c/p\u003e\n\u003cp\u003eTo include chromedriver on the path if it isn't already, make sure you include the chromedriver binary's parent directory. The following line will set the\u0026nbsp;PATH\u0026nbsp;environmental variable its current content, plus an additional path added after the colon:\u003c/p\u003e\n\u003cp\u003e$ export PATH=\"$PATH:/path/to/chromedriver\"\u003c/p\u003e\n\u003cp\u003eWhen chromedriver is available on your path, you should be able to execute the _chromedriver_ executable from any directory.\u003c/p\u003e\n\u003cp\u003eTo instantiate a Chrome/Chromium session, you can do the following:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new ChromeDriver();\u003c/p\u003e\n\u003cp\u003eRemember that you have to set the path to the chromedriver executable. This is possible using the following line:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.chrome.driver\", \"/path/to/chromedriver\");\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :chrome\u003c/p\u003e\n\u003cp\u003eThe chromedriver is implemented as a WebDriver remote server that by exposing Chrome's internal automation proxy interface instructs the browser what to do.\u003c/p\u003e\n\u003cp\u003eFirefox\u003c/p\u003e\n\u003cp\u003eStarting with Selenium 3, Mozilla has taken over implementation of Firefox Driver, with\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/mozilla/geckodriver\"\u003egeckodriver\u003c/a\u003e. The new driver for Firefox is called geckodriver and works with Firefox 48 and newer. Since the Firefox WebDriver is under development, the newer the Firefox version the better the support.\u003c/p\u003e\n\u003cp\u003eAs geckodriver is the new default way of launching Firefox, you can instantiate Firefox in the same way as Selenium 2:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new FirefoxDriver();\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :firefox\u003c/p\u003e\n\u003cp\u003eIf you prefer not to set geckodriver's location using PATH, set the geckodriver binary location programmatically:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.gecko.driver\", \"/path/to/geckodriver\");\u003c/p\u003e\n\u003cp\u003eIt is also possible to set the property at run time:\u003c/p\u003e\n\u003cp\u003emvn test -Dwebdriver.gecko.driver=/path/to/geckodriver\u003c/p\u003e\n\u003cp\u003eIt is currently possible to revert to the older, more feature complete Firefox driver, by installing Firefox\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://ftp.mozilla.org/pub/firefox/releases/47.0.1/\"\u003e47.0.1\u003c/a\u003e\u0026nbsp;or\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://ftp.mozilla.org/pub/firefox/releases/45.0esr/\"\u003e45 ESR\u003c/a\u003e\u0026nbsp;and specifying a desired capability of\u0026nbsp;marionette\u0026nbsp;as\u0026nbsp;false. Later releases of Firefox are no longer compatible.\u003c/p\u003e\n\u003cp\u003eEdge\u003c/p\u003e\n\u003cp\u003eEdge is Microsoft's newest browser, included with Windows 10 and Server 2016. Updates to Edge are bundled with major Windows updates, so you'll need to download a binary which matches the build number of your currently installed build of Windows. The\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/\"\u003eEdge Developer site\u003c/a\u003e\u0026nbsp;contains links to all the available binaries. Bugs against the EdgeDriver implementation can be raised with\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/?page=1\u0026q=webdriver\"\u003eMicrosoft\u003c/a\u003e. If you'd like to run tests against Edge, but aren't running Windows 10, Microsoft offer free VMs for testers on the\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/\"\u003eEdge Developer site\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new EdgeDriver();\u003c/p\u003e\n\u003cp\u003eIf Edge driver is not present in your path, you can set the path using the following line:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.edge.driver\", \"/path/to/edgedriver\");\u003c/p\u003e\n\u003cp\u003eInternet Explorer\u003c/p\u003e\n\u003cp\u003eInternet Explorer was Microsoft's default browser until Windows 10, although it is still included in Windows 10. Internet Explorer Driver is the only driver The Selenium project aims to support the same releases\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://support.microsoft.com/en-gb/help/17454/lifecycle-support-policy-faq-internet-explorer\"\u003eMicrosoft considers current\u003c/a\u003e. Older releases may work, but will be unsupported.\u003c/p\u003e\n\u003cp\u003eWhile the Selenium project provides binaries for both the 32-bit and 64-bit versions of Internet Explorer, there are some\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://jimevansmusic.blogspot.co.uk/2014/09/screenshots-sendkeys-and-sixty-four.html\"\u003elimitations\u003c/a\u003e\u0026nbsp;with Internet Explorer 10 \u0026amp; 11 with the 64-bit driver, but using the 32-bit driver continues to work well. It should be noted that as Internet Explorer preferences are saved against the logged in user's account, some\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration\"\u003eadditional setup is required\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new InternetExplorerDriver();\u003c/p\u003e\n\u003cp\u003eIf Internet Explorer driver is not present in your path, you can set the path using the following line:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.ie.driver\", \"/path/to/iedriver\");\u003c/p\u003e\n\u003cp\u003eMicrosoft also offer a WebDriver binary for\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://www.microsoft.com/en-gb/download/details.aspx?id=44069\"\u003eInternet Explorer 11 on Windows 7 \u0026 8.1\u003c/a\u003e. It has not been updated since 2014 and is based of a draft version of the W3 specification.\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://jimevansmusic.blogspot.co.uk/2014/09/using-internet-explorer-webdriver.html\"\u003eJim Evans\u003c/a\u003e\u0026nbsp;has an excellent writeup on Microsoft's implementation.\u003c/p\u003e\n\u003cp\u003eOpera\u003c/p\u003e\n\u003cp\u003eCurrent releases of Opera are built on top of the Chromium engine, and WebDriver is now supported via the closed-source\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/releases\"\u003eOpera Chromium Driver\u003c/a\u003e, which can be\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#adding_executables_to_your_path\"\u003eadded to your PATH\u003c/a\u003e\u0026nbsp;or as a system property.\u003c/p\u003e\n\u003cp\u003eInstantiating a driver session is similar to Firefox and Chromium:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new OperaDriver();\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :opera\u003c/p\u003e\n\u003cp\u003eSafari\u003c/p\u003e\n\u003cp\u003eStarting with Safari 10 on macOS El Capitan and Sierra, WebDriver support is included with each release of the browser. To enable support:\u003c/p\u003e\n\u003cp\u003e/usr/bin/safaridriver -p 1337\u003c/p\u003e\n\u003cp\u003efrom the terminal for the first time and type your password at the prompt to authorise WebDriver\u003c/p\u003e\n\u003cp\u003eYou can then start a driver session using:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new SafariDriver();\u003c/p\u003e\n\u003cp\u003eThose looking to automate Safari on iOS should look to the\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://appium.io/\"\u003eAppium project\u003c/a\u003e. Whilst Safari was previously available for Windows, Apple has long since dropped support, making it a poor choice of test platform.\u003c/p\u003e\n\u003cp\u003eMock browsers\u003c/p\u003e\n\u003cp\u003eHtmlUnit\u003c/p\u003e\n\u003cp\u003eHtmlUnit is a \"GUI-Less browser for Java programs\". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc. It has JavaScript support and is able to work with AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used. It has been moved to a\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://htmlunit.sourceforge.net/gettingStarted.html\"\u003enew\u003c/a\u003e\u0026nbsp;location.\u003c/p\u003e\n\u003cp\u003eThe source is maintained on svn.\u003c/p\u003e\n\u003cp\u003ePhantomJS\u003c/p\u003e\n\u003cp\u003ePhantomJS is a headless browser based on Webkit, albeit a version much older than that used by Google Chrome or Safari.\u0026nbsp;. Whilst historically a popular choice, it would now be wise to avoid PhantomJS. The project has been unmaintained\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE\"\u003esince the 5th of August\u003c/a\u003e, so whilst the web will continue to change, PhantomJS will not be updated. This was after Google announced the ability to run Chrome headlessly, something also now offered by Mozilla's Firefox.\u003c/p\u003e\n\u003cp\u003eBROWSER LAUNCHING AND MANIPULATION\u003c/p\u003e\n\u003cp\u003eRuby\u003c/p\u003e\n\u003cp\u003eRuby is not installed by default on Windows. Download the latest\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://rubyinstaller.org/downloads\"\u003eversion\u003c/a\u003e\u0026nbsp;and run the installer. You can leave all settings at default values, except at the\u0026nbsp;Installation Destination and Optional Tasks\u0026nbsp;screen check\u0026nbsp;Add Ruby executables to your PATH\u0026nbsp;checkbox. To drive any browser, you have to install selenium-webdriver Ruby gem. To install it, open command prompt and type this:\u003c/p\u003e\n\u003cp\u003e$ gem install selenium-webdriver\u003c/p\u003e\n\u003cp\u003eInternet Explorer\u003c/p\u003e\n\u003cp\u003eInternet Explorer is installed by default on Windows, so no installation is needed. To drive Internet Explorer on Windows, you have to download the latest\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://www.seleniumhq.org/download/\"\u003eInternet Explorer Driver\u003c/a\u003e\u0026nbsp;and put the file into a folder that is in PATH. To find out which directories are in PATH, type\u0026nbsp;echo %PATH%\u0026nbsp;in command prompt.\u003c/p\u003e\n\u003cp\u003e$ echo %PATH% C:\\Ruby200\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem\u003c/p\u003e\n\u003cp\u003eC:\\Ruby200\\bin\u0026nbsp;looks like a good place. Unzip `IEDriverServer` file and move `IEDriverServer.exe` there. This should open a new Internet Explorer window:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.ie.driver\", /path/to/internetexplorerdriver); WebDriver driver = new InternetExplorerDriver();\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :internet_explorer\u003c/p\u003e\n\u003cp\u003eWAITS\u003c/p\u003e\n\u003cp\u003eWebDriver can generally be said to have a blocking API. Because it is an out-of-process library thatinstructs\u0026nbsp;the browser what to do, and because the web platform has an intrinsically asynchronous nature, WebDriver doesn't track the active, real-time state of the DOM. This comes with some challenges that we will discuss here.\u003c/p\u003e\n\u003cp\u003eFrom experience, most intermittents that arise from use of Selenium and WebDriver are connected to\u0026nbsp;race conditions\u0026nbsp;that occur between the browser and the user's instructions. An example could be that the user instructs the browser to navigate to a page, then gets a\u0026nbsp;no such element\u0026nbsp;error when trying to find an element.\u003c/p\u003e\n\u003cp\u003eConsider the following document:\u003c/p\u003e\n\u003cp\u003e\u003c!doctype html\u003e \u003cmeta charset=utf-8\u003e \u003ctitle\u003eRace Condition Example\u003ctitle\u003e \u003cscript\u003e var initialised = false; window.addEventListener(\"load\", function() { var newElement = document.createElement(\"p\"); newElement.textContent = \"Hello from JavaScript!\"; document.body.appendChild(newElement); initialised = true; }); \u003c/script\u003e\u003c/p\u003e\n\u003cp\u003eThe WebDriver instructions might look innocent enough:\u003c/p\u003e\n\u003cp\u003edriver.navigate(\"file:///race_condition.html\") el = driver.find_element_by_tag_name(\"p\") assert el.text == \"Hello from JavaScript!\"\u003c/p\u003e\n\u003cp\u003eThe issue here is that the default\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#page_load_strategy\"\u003epage load strategy\u003c/a\u003e\u0026nbsp;used in WebDriver listens for the\u0026nbsp;document.readyState\u0026nbsp;to change to\u0026nbsp;\"complete\"\u0026nbsp;before returning from the call to\u0026nbsp;navigate. Because the\u0026nbsp;p\u0026nbsp;element is added\u0026nbsp;after\u0026nbsp;the document has completed loading, this WebDriver script\u0026nbsp;might\u0026nbsp;be intermittent. It “might” be intermittent because no guarantees can be made about elements or events that trigger asynchronously without explicitly waiting—or blocking—on those events.\u003c/p\u003e\n\u003cp\u003eFortunately, using the normal instruction set available on the\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#webelement\"\u003eWebElement\u003c/a\u003e\u0026nbsp;interface—such as\u0026nbsp;WebElement.click\u0026nbsp;and\u0026nbsp;WebElement.sendKeys—are guaranteed to be synchronous, in that the function calls won't return (or the callback won't trigger in callback-style languages) until the command has been completed in the browser. The advanced user interaction APIs,\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#keyboard\"\u003eKeyboard\u003c/a\u003e\u0026nbsp;and\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#mouse\"\u003eMouse\u003c/a\u003e, are exceptions as they are explicitly intended as “do what I say” asynchronous commands.\u003c/p\u003e\n\u003cp\u003eWaiting is having the automated task execution elapse a certain amount of time before continuing with the next step.\u003c/p\u003e\n\u003cp\u003eTo overcome the problem of race conditions between the browser and your WebDriver script, most Selenium clients ship with a\u0026nbsp;wait\u0026nbsp;package. When employing a wait, you are using what is commonly referred to as an\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#explicit_wait\"\u003eexplicit wait\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eExplicit waiting\u003c/p\u003e\n\u003cp\u003eExplicit waits\u0026nbsp;are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freeze the thread, until the\u0026nbsp;condition\u0026nbsp;you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.\u003c/p\u003e\n\u003cp\u003eSince explicit waits allow you to wait for a condition to occur, they make a good fit for synchronising the state between the browser and its DOM, and your WebDriver script.\u003c/p\u003e\n\u003cp\u003eTo remedy our buggy instruction set from earlier, we could employ a wait to have the\u0026nbsp;findElement\u0026nbsp;call wait until the dynamically added element from the script has been added to the DOM:\u003c/p\u003e\n\u003cp\u003efrom selenium.webdriver.support.ui import WebDriverWait def document_initialised(driver): return driver.execute_script(\"return initialised\") driver.navigate(\"file:///race_condition.html\") WebDriverWait(driver).until(document_initialised) el = driver.find_element_by_tag_name(\"p\") assert el.text == \"Hello from JavaScript!\"\u003c/p\u003e\n\u003cp\u003eWe pass in the\u0026nbsp;condition\u0026nbsp;as a function reference that the\u0026nbsp;wait\u0026nbsp;will run repeatedly until its return value is truthy. A “truthful” return value is anything that evaluates to boolean true in the language at hand, such as a string, number, a boolean, an object (including a\u0026nbsp;WebElement), or a populated (non-empty) sequence or list. That means an\u0026nbsp;empty list\u0026nbsp;evaluates to false. When the condition is truthful and the blocking wait is aborted, the return value from the condition becomes the return value of the wait.\u003c/p\u003e\n\u003cp\u003eWith this knowledge, and because the wait utility ignores\u0026nbsp;no such element\u0026nbsp;errors by default, we can refactor our instructions to be more concise:\u003c/p\u003e\n\u003cp\u003efrom selenium.webdriver.support.ui import WebDriverWait driver.navigate(\"file:///race_condition.html\") el = WebDriverWait(driver).until(lambda d: return d.find_element_by_tag_name(\"p\")) assert el.text == \"Hello from JavaScript!\"\u003c/p\u003e\n\u003cp\u003eIn that example, we pass in an anonymous function (but we could also define it explicitly as we did earlier so it may be reused). The first and only argument that is passed to our condition is always a reference to our driver object,\u0026nbsp;WebDriver\u0026nbsp;(called\u0026nbsp;d\u0026nbsp;in the example). In a multi-threaded environment, you should be careful to operate on the driver reference passed in to the condition rather than the reference to the driver in the outer scope.\u003c/p\u003e\n\u003cp\u003eBecause the wait will swallow\u0026nbsp;no such element\u0026nbsp;errors that are raised when the element isn't found, the condition will retry until the element is found. Then it will take the return value, a\u0026nbsp;WebElement, and pass it back through to our script.\u003c/p\u003e\n\u003cp\u003eIf the condition fails, e.g. a truthful return value from the condition is never reached, the wait will throw/raise an error/exception called a\u0026nbsp;timeout error.\u003c/p\u003e\n\u003cp\u003eOptions\u003c/p\u003e\n\u003cp\u003eThe wait condition can be customised to match your needs. Sometimes it's unnecessary to wait the full extent of the default timeout, as the penalty for not hitting a successful condition can be expensive.\u003c/p\u003e\n\u003cp\u003eThe wait lets you pass in an argument to override the timeout:\u003c/p\u003e\n\u003cp\u003eWebDriverWait(driver, timeout=3).until(some_condition)\u003c/p\u003e\n\u003cp\u003eExpected conditions\u003c/p\u003e\n\u003cp\u003eBecause it's quite a common occurrence to have to synchronise the DOM and your instructions, most clients also come with a set of predefined\u0026nbsp;expected conditions. As might be obvious by the name, they are conditions that are predefined for frequent wait operations.\u003c/p\u003e\n\u003cp\u003eThe conditions available in the different language bindings vary, but this is a non-exhaustive list of a few:\u003c/p\u003e\n\u003cp\u003ealert is present\u003c/p\u003e\n\u003cp\u003eelement exists\u003c/p\u003e\n\u003cp\u003eelement is visible\u003c/p\u003e\n\u003cp\u003etitle contains\u003c/p\u003e\n\u003cp\u003etitle is\u003c/p\u003e\n\u003cp\u003eelement staleness\u003c/p\u003e\n\u003cp\u003evisible text\u003c/p\u003e\n\u003cp\u003eYou can refer to the API documentation for each client binding to find an exhaustive list of expected conditions:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eJava's\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html\"\u003eorg.openqa.selenium.support.ui.ExpectedConditions\u003c/a\u003e\u0026nbsp;class\u003c/li\u003e\n\u003cli\u003ePython's\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html?highlight=expected\"\u003eselenium.webdriver.support.expected_conditions\u003c/a\u003e\u0026nbsp;class\u003c/li\u003e\n\u003cli\u003e.NET's\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm\"\u003eOpenQA.Selenium.Support.UI.ExpectedConditions\u003c/a\u003e\u0026nbsp;type\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eImplicit waiting\u003c/p\u003e\n\u003cp\u003eThere is a second type of wait that is distinct from\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#explicit_wait\"\u003eexplicit waits\u003c/a\u003e\u0026nbsp;called\u0026nbsp;implicit waiting. By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find\u0026nbsp;any\u0026nbsp;element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.\u003c/p\u003e\n\u003cp\u003eImplicit waiting for elements to appear is disabled by default and will need to be manually enabled on a per-session basis. Mixing\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#explicit_wait\"\u003eexplicit waits\u003c/a\u003e\u0026nbsp;and implicit waiting will cause unintended consequences, namely waits sleeping for the maximum time even if the element is available or condition is true.\u003c/p\u003e\n\u003cp\u003eWarning:\u0026nbsp;Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.\u003c/p\u003e\n\u003cp\u003eAn implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(\"http://somedomain/url_that_delays_loading\"); WebElement myDynamicElement = driver.findElement(By.id(\"myDynamicElement\"));\u003c/p\u003e\n\u003cp\u003eFluentWait\u003c/p\u003e\n\u003cp\u003eFluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.\u003c/p\u003e\n\u003cp\u003eUsers may configure the wait to ignore specific types of exceptions whilst waiting, such as\u0026nbsp;NoSuchElementExceptions when searching for an element on the page.\u003c/p\u003e\n\u003cp\u003e// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait\u003cWebDriver\u003e wait = new FluentWait\u003cWebDriver\u003e(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function\u003cWebDriver, WebElement\u003e() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id(\"foo\")); } });\u003c/p\u003e\n\u003cp\u003eFluentWait\u003cBy\u003e fluentWait = new FluentWait\u003cBy\u003e(By.tagName(\"TEXTAREA\")); fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS); fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); fluentWait.until(new Predicate\u003cBy\u003e() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName(\"TEXTAREA\")).sendKeys(\"text to enter\");\u003c/p\u003e\n\u003cp\u003eSUPPORT CLASSES\u003c/p\u003e\n\u003cp\u003eJAVASCRIPT ALERTS, PROMPTS AND CONFIRMATIONS\u003c/p\u003e\n\u003cp\u003eWebDriver provides an API for working with the three types of native popup message offered by JavaScript. These popups are styled by the browser and offer limited customisation.\u003c/p\u003e\n\u003cp\u003eAlerts\u003c/p\u003e\n\u003cp\u003eThe simplest of these is referred to as an alert, which shows a custom message, and a single button which dismisses the alert, labelled in most browsers as OK. It can also be dismissed in most browsers by pressing the close button, but this will always do the same thing as the OK button.\u0026nbsp;See an example alert.\u003c/p\u003e\n\u003cp\u003eWebDriver can get the text from the popup and accept or dismiss these alerts.\u003c/p\u003e\n\u003cp\u003e//Click the link to activate the alert driver.findElement(By.linkText(\"See an example alert\")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Store the alert text in a variable String text = alert.getText(); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text(\"See an example alert\").click() # Wait for the alert to be displayed and store it in a variable alert = wait.until(expected_conditions.alert_is_present()) # Store the alert text in a variable text = alert.text # Press the OK button alert.accept()\u003c/p\u003e\n\u003cp\u003eConfirm\u003c/p\u003e\n\u003cp\u003eA confirm box is similar to an alert, except the user can also choose to cancel the message.\u0026nbsp;See a sample confirm.\u003c/p\u003e\n\u003cp\u003eThis example also shows a different approach to storing an alert:\u003c/p\u003e\n\u003cp\u003e//Click the link to activate the alert driver.findElement(By.linkText(\"See a sample confirm\")).click(); //Wait for the alert to be displayed wait.until(ExpectedConditions.alertIsPresent()); //Store the alert in a variable Alert alert = driver.switchTo().alert(); //Store the alert in a variable for reuse String text = alert.getText(); //Press the Cancel button alert.dismiss(); # Click the link to activate the alert driver.find_element_by_link_text(\"See a sample confirm\").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = driver.switch_to.alert # Store the alert text in a variable text = alert.text # Press the Cancel button alert.dismiss()\u003c/p\u003e\n\u003cp\u003ePrompt\u003c/p\u003e\n\u003cp\u003ePrompts are similar to confirm boxes, except they also include a text input. Similar to working with form elements, you can use WebDriver's send keys to fill in a response. This will completely replace the placeholder text. Pressing the cancel button will not submit any text.\u0026nbsp;See a sample prompt.\u003c/p\u003e\n\u003cp\u003e//Click the link to activate the alert driver.findElement(By.linkText(\"See a sample prompt\")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Type your message alert.sendKeys(\"Selenium\"); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text(\"See a sample prompt\").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = Alert(driver) # Type your message alert.send_keys(\"Selenium\") # Press the OK button alert.accept()\u003c/p\u003e\n\u003cp\u003eHTTP PROXIES\u003c/p\u003e\n\u003cp\u003ePAGE LOADING STRATEGY\u003c/p\u003e\n\u003cp\u003eWEB ELEMENTS\u003c/p\u003e\n\u003cp\u003eKEYBOARD\u003c/p\u003e\n\u003cp\u003eMOUSE\u003c/p\u003e\n\u003cp\u003eWEBDRIVER\u003c/p\u003e\n\u003cp\u003eSelenium最近的最大变化是包含了WebDriver API。以本地方式作为用户驱动浏览器将在本地或使用Selenium服务器的远程计算机上运行,​​这标志着浏览器自动化方面的一次飞跃。\u003c/p\u003e\n\u003cp\u003eSelenium WebDriver与RC的作用相同,并且包含原始的1.x绑定。它涉及语言绑定和各个浏览器控制代码的实现。这通常被称为WebDriver\u0026nbsp;或有时被称为Selenium 2。\u003c/p\u003e\n\u003cp\u003eSelenium 1.0 + WebDriver = Selenium 2.0\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eWebDriver的设计更简单,更简洁的编程界面,同时解决了Selenium-RC API中的一些限制。\u003c/li\u003e\n\u003cli\u003e与Selenium 1.0相比,WebDriver是一个紧凑的面向对象API。\u003c/li\u003e\n\u003cli\u003e它更有效地驱动浏览器,并克服了Selenium 1的局限性,这些限制影响了我们的功能测试覆盖范围,如文件上传或下载,弹出窗口和对话框屏障。\u003c/li\u003e\n\u003cli\u003eWebDriver克服了Selenium RC的\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://en.wikipedia.org/wiki/Same-origin_policy\"\u003e单主机起源策略\u003c/a\u003e的局限性\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://en.wikipedia.org/wiki/Same-origin_policy\"\u003e。\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003e驱动要求\u003c/p\u003e\n\u003cp\u003e通过WebDriver,Selenium支持市场上所有主流浏览器,如Chrom(ium),Firefox,Internet Explorer,Opera和Safari。在可能的情况下,WebDriver使用浏览器对自动化的内置支持来驱动浏览器,但并非所有浏览器都支持远程控制。\u003c/p\u003e\n\u003cp\u003eWebDriver的目标是尽可能模仿真实用户与浏览器的交互。在不同的浏览器中,这是可能的。有关不同驱动程序特性的更多详细信息,请参阅\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/drivers.html\"\u003e驱动\u003c/a\u003e程序特性。\u003c/p\u003e\n\u003cp\u003e即使所有驱动程序共享一个用于控制浏览器的面向用户的界面,但它们在设置浏览器会话方面略有不同。由于许多驱动程序实现由第三方提供,因此它们不包含在标准的Selenium发行版中。\u003c/p\u003e\n\u003cp\u003e驱动程序实例化,配置文件管理以及各种浏览器特定设置是根据浏览器具有不同要求的参数示例。本节介绍让您开始使用不同浏览器的基本要求。\u003c/p\u003e\n\u003cp\u003e将可执行文件添加到您的PATH\u003c/p\u003e\n\u003cp\u003e大多数驱动程序需要一个额外的可执行文件让Selenium与浏览器通信。您可以在启动WebDriver之前手动指定可执行文件的位置,但这可能会使测试的可移植性降低,因为可执行文件需要位于每台计算机的相同位置,或者包含在测试代码库中。\u003c/p\u003e\n\u003cp\u003e通过将包含WebDriver二进制文件的文件夹添加到系统路径中,Selenium将能够找到其他二进制文件,而无需您的测试代码来查找驱动程序的确切位置。\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003cp\u003e创建一个目录来放置浏览器驱动可执行文件,例如\u0026nbsp;C:\\ WebDriver \\ bin或/ opt / WebDriver / bin\u003c/p\u003e\u003c/li\u003e\n\u003cli\u003e\u003cp\u003e将该目录添加到系统环境变量PATH中:\u003c/p\u003e\u003c/li\u003e\n\u003cli\u003e\u003cp\u003e在Windows上 - 以管理员身份打开命令提示符,然后运行以下命令将该目录永久地添加到您计算机上所有用户的路径中:\u003c/p\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003esetx /m path “%path%;C:\\WebDriver \\bin\\”\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e在macOS和Linux上使用bash用户 - 在终端中:\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eexport PATH=$PATH:/opt/WebDriver/bin \u0026gt;\u0026gt; 〜/.profile\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e您现在可以验证您的更改是否有效。关闭所有打开的命令提示符并打开一个新提示符。在上一步创建的文件夹中输入其中一个二进制文件的名称,例如:\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003echromedriver\u003c/p\u003e\n\u003cp\u003e。如果您的PATH配置正确,您会看到一些与驱动程序启动有关的输出:\u003c/p\u003e\n\u003cp\u003eStarting ChromeDriver 2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1) on port 9515 Only local connections are allowed.\u003c/p\u003e\n\u003cp\u003e按下,您可以重新获得对命令提示符的控制Ctrl + C。\u003c/p\u003e\n\u003cp\u003e快速参考\u003c/p\u003e\n\u003cp\u003e浏览器\u003c/p\u003e\n\u003cp\u003e支持的OS\u003c/p\u003e\n\u003cp\u003e由…维护\u003c/p\u003e\n\u003cp\u003e下载\u003c/p\u003e\n\u003cp\u003e问题跟踪器\u003c/p\u003e\n\u003cp\u003e铬/铬\u003c/p\u003e\n\u003cp\u003eWindows\u0026nbsp;\u003c/p\u003e\n\u003cp\u003emacOS\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eLinux\u003c/p\u003e\n\u003cp\u003e谷歌\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://chromedriver.storage.googleapis.com/index.html\"\u003e下载\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://bugs.chromium.org/p/chromedriver/issues/list\"\u003e问题\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e火狐\u003c/p\u003e\n\u003cp\u003eWindows\u0026nbsp;\u003c/p\u003e\n\u003cp\u003emacOS\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eLinux\u003c/p\u003e\n\u003cp\u003eMozilla\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/mozilla/geckodriver/releases\"\u003e下载\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/mozilla/geckodriver/issues\"\u003e问题\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e边缘\u003c/p\u003e\n\u003cp\u003eWindows 10\u003c/p\u003e\n\u003cp\u003e微软\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/\"\u003e下载\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/?page=1\u0026q=webdriver\"\u003e问题\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eIE浏览器\u003c/p\u003e\n\u003cp\u003e视窗\u003c/p\u003e\n\u003cp\u003e硒项目\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://selenium-release.storage.googleapis.com/index.html\"\u003e下载\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/SeleniumHQ/selenium/labels/D-IE\"\u003e问题\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e苹果浏览器\u003c/p\u003e\n\u003cp\u003emacOS El Capitan和更新的\u003c/p\u003e\n\u003cp\u003e苹果\u003c/p\u003e\n\u003cp\u003e内置\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://bugreport.apple.com/logon\"\u003e问题\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e歌剧\u003c/p\u003e\n\u003cp\u003eWindows\u0026nbsp;\u003c/p\u003e\n\u003cp\u003emacOS\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eLinux\u003c/p\u003e\n\u003cp\u003e歌剧\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/releases\"\u003e下载\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/issues\"\u003e问题\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e铬/铬\u003c/p\u003e\n\u003cp\u003e要驱动Chrome或Chromium,您必须下载\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://sites.google.com/a/chromium.org/chromedriver/downloads\"\u003echromedriver\u003c/a\u003e\u0026nbsp;并将其放入系统路径中的文件夹。\u003c/p\u003e\n\u003cp\u003e在Linux或macOS上,这意味着修改PATH环境变量。您可以通过执行以下命令来查看以冒号分隔的目录是否构成系统的路径:\u003c/p\u003e\n\u003cp\u003e$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u003c/p\u003e\n\u003cp\u003e如果路径中尚未包含chromedriver,请确保包含chromedriver二进制文件的父目录。以下行将设置PATH环境变量的当前内容,并在冒号后添加一个额外的路径:\u003c/p\u003e\n\u003cp\u003e$ export PATH=\"$PATH:/path/to/chromedriver\"\u003c/p\u003e\n\u003cp\u003e当您的路径上有可用的chromedriver时,您应该能够从任何目录执行_chromedriver_可执行文件。\u003c/p\u003e\n\u003cp\u003e要实例化Chrome / Chromium会话,您可以执行以下操作:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new ChromeDriver();\u003c/p\u003e\n\u003cp\u003e请记住,您必须将路径设置为chromedriver可执行文件。这可以使用以下行:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.chrome.driver\", \"/path/to/chromedriver\");\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :chrome\u003c/p\u003e\n\u003cp\u003echromedriver被当作WebDriver远程服务器,它通过公开chrome的内部自动代理接口来指挥浏览器做什么\u003c/p\u003e\n\u003cp\u003e火狐\u003c/p\u003e\n\u003cp\u003e从Selenium 3开始,Mozilla利用\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/mozilla/geckodriver\"\u003egeckodriver\u003c/a\u003e接管了Firefox Driver的实现。Firefox的新驱动称为geckodriver,适用于Firefox 48及更新版本。由于Firefox WebDriver正在开发中,Firefox版本越新越好。\u003c/p\u003e\n\u003cp\u003e由于geckodriver是启动Firefox的新默认方式,因此您可以像Selenium 2一样实例化Firefox:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new FirefoxDriver();\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :firefox\u003c/p\u003e\n\u003cp\u003e如果您不想使用PATH设置geckodriver的位置,请以编程方式设置geckodriver二进制位置:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.gecko.driver\", \"/path/to/geckodriver\");\u003c/p\u003e\n\u003cp\u003e也可以在运行时设置属性:\u003c/p\u003e\n\u003cp\u003emvn test -Dwebdriver.gecko.driver=/path/to/geckodriver\u003c/p\u003e\n\u003cp\u003e目前有可能通过安装Firefox\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://ftp.mozilla.org/pub/firefox/releases/47.0.1/\"\u003e47.0.1\u003c/a\u003e\u0026nbsp;或\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://ftp.mozilla.org/pub/firefox/releases/45.0esr/\"\u003e45 ESR\u003c/a\u003e\u0026nbsp;并将所需的木偶功能指定为\u0026nbsp;false来恢复到较老的,功能更完整的Firefox驱动程序\u0026nbsp;。Firefox的后续版本不再兼容。\u003c/p\u003e\n\u003cp\u003eEdge\u003c/p\u003e\n\u003cp\u003eEdge是Microsoft最新的浏览器,包含在Windows 10和Server 2016中。Edge的更新与主要Windows更新捆绑在一起,因此您需要下载与您当前安装的Windows版本号相匹配的二进制文件。该\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/\"\u003e边缘开发人员网站\u003c/a\u003e\u0026nbsp;包含指向所有可用的二进制文件。\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/?page=1\u0026q=webdriver\"\u003e微软\u003c/a\u003e可以提出针对EdgeDriver实施的错误\u0026nbsp;。如果您希望针对Edge运行测试,但不运行Windows 10,Microsoft会在\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/\"\u003eEdge开发人员网站\u003c/a\u003e上为测试人员提供免费虚拟机。\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new EdgeDriver();\u003c/p\u003e\n\u003cp\u003e如果Edge驱动程序不在您的路径中,则可以使用以下行设置路径:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.edge.driver\", \"/path/to/edgedriver\");\u003c/p\u003e\n\u003cp\u003eIE浏览器\u003c/p\u003e\n\u003cp\u003e在Windows 10之前,Internet Explorer是微软的默认浏览器,尽管它仍然包含在Windows 10中。Internet Explorer驱动程序是唯一的驱动程序Selenium项目旨在支持\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://support.microsoft.com/en-gb/help/17454/lifecycle-support-policy-faq-internet-explorer\"\u003eMicrosoft认为的最新\u003c/a\u003e版本\u0026nbsp;。旧版本可能有效,但不受支持。\u003c/p\u003e\n\u003cp\u003e虽然Selenium项目为32位和64位版本的Internet Explorer提供了二进制文件,但Internet Explorer 10和11在64位驱动程序方面存在一些\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://jimevansmusic.blogspot.co.uk/2014/09/screenshots-sendkeys-and-sixty-four.html\"\u003e局限性\u003c/a\u003e,但使用32位驱动程序仍能正常运行。应该注意的是,由于Internet Explorer首选项是针对登录用户的帐户保存的,\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration\"\u003e因此需要进行\u003c/a\u003e一些\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration\"\u003e额外的设置\u003c/a\u003e。\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new InternetExplorerDriver();\u003c/p\u003e\n\u003cp\u003e如果Internet Explorer驱动程序不在您的路径中,则可以使用以下行来设置路径:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.ie.driver\", \"/path/to/iedriver\");\u003c/p\u003e\n\u003cp\u003e微软还为\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://www.microsoft.com/en-gb/download/details.aspx?id=44069\"\u003eWindows 7和8.1上的Internet Explorer 11\u003c/a\u003e提供WebDriver二进制文件。它自2014年以来一直未更新,并基于W3规范的草案版本。\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://jimevansmusic.blogspot.co.uk/2014/09/using-internet-explorer-webdriver.html\"\u003eJim Evans\u003c/a\u003e对微软的实施有很好的评论。\u003c/p\u003e\n\u003cp\u003eOpera\u003c/p\u003e\n\u003cp\u003eOpera的当前版本建立在Chromium引擎之上,WebDriver现在通过闭源\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/releases\"\u003eOpera Chromium驱动程序提供支持\u003c/a\u003e,该\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://github.com/operasoftware/operachromiumdriver/releases\"\u003e驱动程序\u003c/a\u003e可以\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#adding_executables_to_your_path\"\u003e添加到PATH中\u003c/a\u003e\u0026nbsp;或作为系统属性。\u003c/p\u003e\n\u003cp\u003e实例化驱动程序会话与Firefox和Chromium类似:\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new OperaDriver();\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :opera\u003c/p\u003e\n\u003cp\u003eSafari\u003c/p\u003e\n\u003cp\u003e从macOS El Capitan和Sierra上的Safari 10开始,每个浏览器版本都包含WebDriver支持。启用支持:\u003c/p\u003e\n\u003cp\u003e/ usr / bin / safaridriver -p 1337\u003c/p\u003e\n\u003cp\u003e从终端第一次,并在提示输入您的密码授权WebDriver\u003c/p\u003e\n\u003cp\u003e然后可以使用以下命令启动驱动程序会话\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new SafariDriver();\u003c/p\u003e\n\u003cp\u003e那些希望在iOS上自动化Safari的人应该看看\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://appium.io/\"\u003eAppium项目\u003c/a\u003e。虽然以前Safari可用于Windows,但Apple早已不再维护,使其成为测试平台的糟糕选择。\u003c/p\u003e\n\u003cp\u003e模拟浏览器\u003c/p\u003e\n\u003cp\u003eHtmlUnit\u003c/p\u003e\n\u003cp\u003eHtmlUnit是一个“Java程序的无GUI浏览器”。它模拟HTML文档,并提供一个API,使您可以调用页面,填写表单,单击链接等。它支持JavaScript,可以使用AJAX库,根据所用配置模拟Chrome,Firefox或Internet Explorer。它已被移到一个\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://htmlunit.sourceforge.net/gettingStarted.html\"\u003e新的\u003c/a\u003e位置。\u003c/p\u003e\n\u003cp\u003e源代码保存在svn上。\u003c/p\u003e\n\u003cp\u003ePhantomJS\u003c/p\u003e\n\u003cp\u003ePhantomJS是一款基于Webkit的无头浏览器,尽管它的版本比Google Chrome或Safari使用的版本要早得多。。虽然历史上流行的选择,但避免PhantomJS现在是明智的。该项目\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE\"\u003e自8月5日以来\u003c/a\u003e一直未得到维护\u0026nbsp;,因此虽然网络将继续发生变化,但PhantomJS不会更新。这是在谷歌宣布无线运行Chrome的能力之后,现在Mozilla的Firefox也提供了一些功能。\u003c/p\u003e\n\u003cp\u003e浏览器启动和操作\u003c/p\u003e\n\u003cp\u003eRuby\u003c/p\u003e\n\u003cp\u003e在Windows上默认不安装Ruby。下载最新\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://rubyinstaller.org/downloads\"\u003e版本\u003c/a\u003e并运行安装程序。您可以将所有设置保留为默认值,除了在“\u0026nbsp;安装目标”和“可选任务”屏幕中检查将Ruby可执行文件添加到PATH复选框。要驱动任何浏览器,您必须安装selenium-webdriver Ruby gem。要安装它,请打开命令提示符并输入:\u003c/p\u003e\n\u003cp\u003e$ gem install selenium-webdriver\u003c/p\u003e\n\u003cp\u003eIE浏览器\u003c/p\u003e\n\u003cp\u003eInternet Explorer默认安装在Windows上,因此不需要安装。要在Windows上驱动Internet Explorer,必须下载最新的\u003ca rel=\"nofollow noopener noreferrer\" href=\"http://www.seleniumhq.org/download/\"\u003eInternet Explorer驱动程序\u003c/a\u003e,并将该文件放入PATH中的文件夹中。要找出PATH中的哪些目录,请在命令提示符下键入echo%PATH%。\u003c/p\u003e\n\u003cp\u003e$ echo %PATH% C:\\Ruby200\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem\u003c/p\u003e\n\u003cp\u003eC:\\ Ruby200 \\ bin看起来像个好地方。解压缩`IEDriverServer`文件并将`IEDriverServer.exe`移动到那里。这应该打开一个新的Internet Explorer窗口:\u003c/p\u003e\n\u003cp\u003eSystem.setProperty(\"webdriver.ie.driver\", /path/to/internetexplorerdriver); WebDriver driver = new InternetExplorerDriver();\u003c/p\u003e\n\u003cp\u003erequire \"selenium-webdriver\" driver = Selenium::WebDriver.for :internet_explorer\u003c/p\u003e\n\u003cp\u003eWAITS功能\u003c/p\u003e\n\u003cp\u003e通常可以说WebDriver具有阻塞API。由于它是一个进程外库,它\u0026nbsp;指示浏览器执行什么操作,并且因为Web平台具有内在的异步特性,所以WebDriver不会跟踪DOM的活动实时状态。这带来了一些挑战,我们将在这里讨论。。\u003c/p\u003e\n\u003cp\u003e根据经验,使用Selenium和WebDriver产生的大部分间歇性事件都与浏览器和用户指令之间出现的争用条件相关联。一个例子可能是用户指示浏览器导航到页面,然后在尝试查找元素时没有发现此类元素错误。\u003c/p\u003e\n\u003cp\u003e参考以下文件:\u003c/p\u003e\n\u003cp\u003e\u003c!doctype html\u003e \u003cmeta charset=utf-8\u003e \u003ctitle\u003eRace Condition Example\u003ctitle\u003e \u003cscript\u003e var initialised = false; window.addEventListener(\"load\", function() { var newElement = document.createElement(\"p\"); newElement.textContent = \"Hello from JavaScript!\"; document.body.appendChild(newElement); initialised = true; }); \u003c/script\u003e\u003c/p\u003e\n\u003cp\u003eWebDriver指令可能看起来无辜:\u003c/p\u003e\n\u003cp\u003edriver.navigate(\"file:///race_condition.html\") el = driver.find_element_by_tag_name(\"p\") assert el.text == \"Hello from JavaScript!\"\u003c/p\u003e\n\u003cp\u003e这里的问题是WebDriver中使用的默认页面加载策略监听文档。readyState更改为“complete”,然后从调用返回导航。因为p元素是在文档完成加载之后添加的,所以这个WebDriver脚本可能是间歇性的。它“可能”是间歇性的,因为不能对异步触发的元素或事件作出保证,而不需要显式地等待或阻塞这些事件。\u003c/p\u003e\n\u003cp\u003e幸运的是,使用\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#webelement\"\u003eWebElement\u003c/a\u003e接口上可用的普通指令集(例如WebElement.click和WebElement.sendKeys)保证是同步的,因为函数调用不会返回(或者回调将不会以回调式触发语言),直到浏览器中的命令完成。高级用户交互API\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#keyboard\"\u003e键盘\u003c/a\u003e和\u0026nbsp;\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#mouse\"\u003e鼠标\u003c/a\u003e是例外,因为它们显式地用作“按我说的做”异步命令。\u003c/p\u003e\n\u003cp\u003e在继续下一步之前,等待会让自动任务执行过去一段时间。\u003c/p\u003e\n\u003cp\u003e为了克服浏览器和WebDriver脚本之间的竞争状况问题,大多数Selenium客户端都附带一个等待包。当采用等待时,您正在使用通常被称为\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#explicit_wait\"\u003e显式等待的内容\u003c/a\u003e。\u003c/p\u003e\n\u003cp\u003e显式等待\u003c/p\u003e\n\u003cp\u003eSelenium客户端可以使用显式等待来使用命令式的过程语言。它们允许您的代码停止程序执行,或者冻结线程,直到您通过的条件得到解决。以特定频率调用条件,直到等待超时。这意味着只要条件返回一个虚假值,它就会继续尝试和等待。\u003c/p\u003e\n\u003cp\u003e由于显式等待允许您等待条件发生,因此它们非常适合于同步浏览器及其DOM和WebDriver脚本之间的状态。\u003c/p\u003e\n\u003cp\u003e为了补救我们早期的错误指令集,我们可以等待findElement调用等待,直到脚本中动态添加的元素被添加到DOM:\u003c/p\u003e\n\u003cp\u003efrom selenium.webdriver.support.ui import WebDriverWait def document_initialised(driver): return driver.execute_script(\"return initialised\") driver.navigate(\"file:///race_condition.html\") WebDriverWait(driver).until(document_initialised) el = driver.find_element_by_tag_name(\"p\") assert el.text == \"Hello from JavaScript!\"\u003c/p\u003e\n\u003cp\u003e我们将条件作为函数引用传递,等待将重复运行,直到其返回值为真。一个“真实的”返回值是任何在手头语言中评估为boolean true的东西,如字符串,数字,布尔值,对象(包括WebElement)或填充(非空)序列或列表。这意味着一个空列表的计算结果为false。当条件是真实的并且阻塞等待被中止时,来自条件的返回值变成等待的返回值。\u003c/p\u003e\n\u003cp\u003e有了这些知识,并且由于等待实用程序默认情况下不会忽略此类元素错误,所以我们可以将我们的指示重构得更加简洁:\u003c/p\u003e\n\u003cp\u003efrom selenium.webdriver.support.ui import WebDriverWait driver.navigate(\"file:///race_condition.html\") el = WebDriverWait(driver).until(lambda d: return d.find_element_by_tag_name(\"p\")) assert el.text == \"Hello from JavaScript!\"\u003c/p\u003e\n\u003cp\u003e在这个例子中,我们传入一个匿名函数(但我们也可以像前面那样明确地定义它,以便它可以被重用)。传递给我们的条件的第一个也是唯一的参数始终是对我们的驱动程序对象WebDriver\u0026nbsp;(在该示例中称为d)的引用。在多线程环境中,您应该小心地对传入条件的驱动程序引用进行操作,而不是在外部范围中对驱动程序的引用。\u003c/p\u003e\n\u003cp\u003e因为等待将不会消除元素未找到时引发的此类元素错误,则条件将重试,直到找到该元素。然后它将获得返回值,一个WebElement,并将其传回给我们的脚本。\u003c/p\u003e\n\u003cp\u003e如果条件失败,例如从条件得到的真实返回值永远不会达到,那么等待将抛出/引发称为超时错误的错误/异常。\u003c/p\u003e\n\u003cp\u003e选项\u003c/p\u003e\n\u003cp\u003e等待条件可以定制以满足您的需求。有时没有必要等待默认超时的全部范围,因为没有达到成功条件的代价会很昂贵。\u003c/p\u003e\n\u003cp\u003e等待让你传入一个参数来覆盖超时:\u003c/p\u003e\n\u003cp\u003eWebDriverWait(driver, timeout=3).until(some_condition)\u003c/p\u003e\n\u003cp\u003e预期条件\u003c/p\u003e\n\u003cp\u003e因为必须同步DOM和您的指令是很常见的事情,所以大多数客户还会附带一组预定义的预期条件。由名称可能很明显,它们是为频繁等待操作而预定义的条件。\u003c/p\u003e\n\u003cp\u003e不同语言绑定中可用的条件各不相同,但这是一个非穷举的列表:\u003c/p\u003e\n\u003cp\u003e警报存在\u003c/p\u003e\n\u003cp\u003e元素存在\u003c/p\u003e\n\u003cp\u003e元素是可见的\u003c/p\u003e\n\u003cp\u003e标题包含\u003c/p\u003e\n\u003cp\u003e标题是\u003c/p\u003e\n\u003cp\u003e元素陈旧\u003c/p\u003e\n\u003cp\u003e可见的文本\u003c/p\u003e\n\u003cp\u003e您可以参考每个客户端绑定的API文档以查找预期条件的详尽列表:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eJava的\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html\"\u003eorg.openqa.selenium.support.ui.ExpectedConditions\u003c/a\u003e类\u003c/li\u003e\n\u003cli\u003ePython的\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html?highlight=expected\"\u003eselenium.webdriver.support.expected_conditions\u003c/a\u003e类\u003c/li\u003e\n\u003cli\u003e.NET的\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm\"\u003eOpenQA.Selenium.Support.UI.ExpectedConditions\u003c/a\u003e类型\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003e隐含的等待\u003c/p\u003e\n\u003cp\u003e还有第二种类型的等待与\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#explicit_wait\"\u003e显式等待\u003c/a\u003e不同,\u0026nbsp;称为隐式等待。通过隐式等待,WebDriver在尝试查找任何元素时轮询DOM一段时间。当网页上的某些元素不可立即使用并需要一些时间加载时,这可能很有用。\u003c/p\u003e\n\u003cp\u003e默认情况下隐式等待元素出现,并且需要在每个会话的基础上手动启用。混合\u003ca rel=\"nofollow noopener noreferrer\" href=\"https://seleniumhq.github.io/docs/wd.html#explicit_wait\"\u003e明确的等待\u003c/a\u003e和隐含的等待将导致意想不到的后果,即等待最大时间睡眠,即使元素可用或条件为真。\u003c/p\u003e\n\u003cp\u003e警告:\u0026nbsp;不要混合隐式和显式等待。这样做会导致无法预测的等待时间。例如,设置10秒的隐式等待和15秒的明确等待可能会导致20秒后发生超时。\u003c/p\u003e\n\u003cp\u003e隐含的等待是告诉WebDriver在尝试查找一个或多个元素(如果它们不是立即可用的)时轮询DOM一段时间。默认设置为0,意味着禁用。一旦设置,隐含的等待就设置为会话的整个生命周期。\u003c/p\u003e\n\u003cp\u003eWebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(\"http://somedomain/url_that_delays_loading\"); WebElement myDynamicElement = driver.findElement(By.id(\"myDynamicElement\"));\u003c/p\u003e\n\u003cp\u003eFluentWait\u003c/p\u003e\n\u003cp\u003eFluentWait实例定义了等待条件的最长时间以及检查条件的频率。\u003c/p\u003e\n\u003cp\u003e用户可以配置等待,以在等待时忽略特定类型的异常,例如NoSuchElementException在页面上搜索元素时。\u003c/p\u003e\n\u003cp\u003e// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait\u003cWebDriver\u003e wait = new FluentWait\u003cWebDriver\u003e(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function\u003cWebDriver, WebElement\u003e() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id(\"foo\")); } });\u003c/p\u003e\n\u003cp\u003eFluentWait\u003cBy\u003e fluentWait = new FluentWait\u003cBy\u003e(By.tagName(\"TEXTAREA\")); fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS); fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); fluentWait.until(new Predicate\u003cBy\u003e() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName(\"TEXTAREA\")).sendKeys(\"text to enter\");\u003c/p\u003e\n\u003cp\u003e支持类\u003c/p\u003e\n\u003cp\u003eJAVASCRIPT警报,提示和确认\u003c/p\u003e\n\u003cp\u003eWebDriver提供了一个用于处理JavaScript提供的三种本地弹出消息的API。这些弹出窗口由浏览器设置并提供有限的自定义。\u003c/p\u003e\n\u003cp\u003eAlerts\u003c/p\u003e\n\u003cp\u003e其中最简单的就是一个警报,它显示了一个自定义消息,以及一个按钮,它解除了警报,在大多数浏览器中标记为OK。它也可以通过按下关闭按钮在大多数浏览器中被解散,但是这总是和OK按钮一样。查看示例警报。\u003c/p\u003e\n\u003cp\u003eWebDriver可以从弹出窗口中获取文本并接受或拒绝这些警报。\u003c/p\u003e\n\u003cp\u003e//Click the link to activate the alert driver.findElement(By.linkText(\"See an example alert\")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Store the alert text in a variable String text = alert.getText(); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text(\"See an example alert\").click() # Wait for the alert to be displayed and store it in a variable alert = wait.until(expected_conditions.alert_is_present()) # Store the alert text in a variable text = alert.text # Press the OK button alert.accept()\u003c/p\u003e\n\u003cp\u003eConfirm\u003c/p\u003e\n\u003cp\u003e确认框与提示类似,除了用户也可以选择取消消息。查看样本确认。\u003c/p\u003e\n\u003cp\u003e此示例还显示了一种用于存储警报的不同方法:\u003c/p\u003e\n\u003cp\u003e//Click the link to activate the alert driver.findElement(By.linkText(\"See a sample confirm\")).click(); //Wait for the alert to be displayed wait.until(ExpectedConditions.alertIsPresent()); //Store the alert in a variable Alert alert = driver.switchTo().alert(); //Store the alert in a variable for reuse String text = alert.getText(); //Press the Cancel button alert.dismiss(); # Click the link to activate the alert driver.find_element_by_link_text(\"See a sample confirm\").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = driver.switch_to.alert # Store the alert text in a variable text = alert.text # Press the Cancel button alert.dismiss()\u003c/p\u003e\n\u003cp\u003ePrompt\u003c/p\u003e\n\u003cp\u003e提示与确认框相似,除了它们还包含文本输入。与使用表单元素类似,您可以使用WebDriver的发送键来填写响应。这将完全取代占位符文本。按取消按钮不会提交任何文本。\u0026nbsp;查看示例提示。\u003c/p\u003e\n\u003cp\u003e//Click the link to activate the alert driver.findElement(By.linkText(\"See a sample prompt\")).click(); //Wait for the alert to be displayed and store it in a variable Alert alert = wait.until(ExpectedConditions.alertIsPresent()); //Type your message alert.sendKeys(\"Selenium\"); //Press the OK button alert.accept(); # Click the link to activate the alert driver.find_element_by_link_text(\"See a sample prompt\").click() # Wait for the alert to be displayed wait.until(expected_conditions.alert_is_present()) # Store the alert in a variable for reuse alert = Alert(driver) # Type your message alert.send_keys(\"Selenium\") # Press the OK button alert.accept()\u003c/p\u003e\n\u003cp\u003eHTTP代理\u003c/p\u003e\n\u003cp\u003e页面加载策略\u003c/p\u003e\n\u003cp\u003eWEB元素\u003c/p\u003e\n\u003cp\u003e键盘\u003c/p\u003e\n\u003cp\u003e鼠标\u003c/p\u003e"},"seo":{"title":"F WebDriver and 环境配置","description":"https://seleniumhq.github.io/docs/wd.html\nWEBDRIVER\nThe biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on","image":"","url":"https://v2as.com/article/22721bca-b928-4959-add5-a2042074a7fe","keywords":["WebDriver","driver","alert","Selenium","wait","浏览器","text","PATH","Windows","element"]},"viewsCount":1,"promotionList":[{"title":"阿里云服务器","image":"https://article.cdnof.com/promotion/aliyun.jpg","link":"https://qd.rs/aliyun"},{"title":"腾讯云服务器","image":"https://article.cdnof.com/promotion/tencent.jpg","link":"https://qd.rs/tencent"},{"title":"七牛云服务器","image":"https://article.cdnof.com/promotion/qiniu.png","link":"https://qd.rs/qiniu"}],"similarKeywordsList":[{"article_id":"2f80d1a6-b248-4bbe-bee3-f342411680b4","title":"selenium+python处理Alert弹窗","link":"","description":"from selenium import webdriver\nimport win32api\nimport win32con\nfrom time import sleep\ndriver = webdriver.Ie()\n\nimport time\nfrom selenium import webdriver\n\ndriver.maximize_window()\ndriver.implicitly_w","image":"","keywords":["弹窗","alert","driver","selenium","win32api","python","event","keybd","import","switch"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"080b1cbb-ae2f-4602-a55d-6b6371986b5a","title":"Selenium_使用switch_to.alert处理弹窗(14)","link":"","description":"与switch_to.window 和 switch_to.frame 相比,switch_to.alert的alert方法使用了@property 装饰器,所以在使用时alert被当成属性调用。\n演示如下\n\nfrom selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.maximize_window()\ndriver.get","image":"","keywords":["弹窗","alert","switch","Selenium","driver","处理","window","selenium","adsbygoogle","python"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"f6d70ae9-0d23-41fe-8979-f9a2441037a5","title":"Python3+Selenium3自动化测试-(六)","link":"","description":"这里来说一说selenium中的等待方式,其实在webdriver只有两种类型等待方式,显式等待和隐式等待,之前是在程序运行过程中使用time模块中的sleep进行代码的休眠进行强制等待,是显式等待中的一种极端情况。\n\n通过time模块中sleep进行代码的暂停,但是实际使用过程中,如果都以sleep进行控制严重影响了程序的运行。\n\n# coding=utf-8\n# 强制等待——代码休眠\nimpo","image":"https://article.cdnof.com/2307/ed4b621c-6b32-4177-8d7f-0ca90dfb2ae4.png","keywords":["selenium","element","元素","webdriver","import","Selenium3","Python3","等待","driver","10"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"534eb274-138c-47f3-bc2b-238c90c5e84c","title":"Selenium4+Python3系列(十) - Page Object设计模式","link":"","description":"前言\nPage Object(PO)模式,是Selenium实战中最为流行,并且被自动化测试同学所熟悉和推崇的一种设计模式之一。在设计测试时,把页面元素定位和元素操作方法按照页面抽象出来,分离成一定的对象,然后再进行组织。\n相信每个做自动化测试的同学,一定会遇到这样一个非常头疼的问题,那就是页面变化,如果没有使用Page Object设计模式,这就意味着以前的定位元素方法不能用了,需要重新修改元素","image":"","keywords":["self","Page","Object","设计模式","login","driver","元素","页面","def","封装"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"a6d3011e-6fe5-45cd-a4c8-d625f816964f","title":"selenium------关于switch_to的用法场景","link":"","description":"基于python3的语法,driver.switch_to_alert()的表达会出现中划线,因此需要把后面的下划线改为点.一、目前接触到的switch_to的用法包括以下几种:1、 切换到制定的window_name页面\ndriver.switch_to.window(window_name)  2、   切换到alert弹窗\ndriver.switch_to.alert()          ","image":"","keywords":["selenium","driver","switch","window","python","用法","element","webdriver","import","Selenium"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"976f7694-c519-4463-9df0-89eb8b18d418","title":"Selenium和PhantomJS","link":"","description":"Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上,它支持所有主流的浏览器(包括PhantomJS这些无界面的浏览器)。\nSelenium 可以根据我们的指令,让浏览器自动加载页面,获取需要的数据,甚至页面截屏,或者判断网站上某些动作是否发生。\nSelenium 自己不","image":"","keywords":["driver","element","Selenium","selenium","By","浏览器","id","webdriver","PhantomJS","cheese"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"8093b371-20a2-4a56-baaa-a99c5037e154","title":"C#爬虫(03):使用Selenium","link":"","description":"Selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。\n1、Selenium Webdriver(也就是Selenium2,Selenium3)和Selenium RC(Selenium 1)一样提供了web自动化的各种语言调用接口库。相比Selenium RC,Selenium WebDriver的编程接口更加直观易懂,也更加简练","image":"https://article.cdnof.com/2307/9b7afdc3-f280-478c-b47c-911ffb87afdb.png","keywords":["driver","Selenium","Console","WriteLine","By","元素","var","FindElement","爬虫","浏览器"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"3f0d13e8-565e-45b2-96ad-8b42524b5a9f","title":"python selenium表单定位","link":"","description":"在Web应用中经常会遇到frame/iframe 表单嵌套页面的应用,WebDriver 只能在一个页面上对元素识别与定位,对于frame/iframe 表单内嵌页面上的元素无法直接定位。这时就需要通过switch_to.frame()方法将当前定位的主体切换为frame/iframe 表单的内嵌页面中:如下两个例子:1 、 163邮箱登入界面:https://mail.163.com/2 、电信","image":"https://article.cdnof.com/2307/0580ddca-5606-4c54-b586-7c7055de0781.png","keywords":["表单","selenium","定位","页面","id","元素","iframe","python","element","webdriver"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"ca83fcfb-03f4-4908-85b3-1dba13889498","title":"Python-selenium显示等待","link":"","description":"#coding=utf-8\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support import expected_conditions as EC\nfrom selenium.webdriver.support.wait import We","image":"","keywords":["driver","等待","WebDriverWait","selenium","元素","EC","10","element","By","webdriver"],"created_at":"0001-01-01T00:00:00Z"},{"article_id":"8c9f5349-7cb2-4b2f-8dae-6e6023f65e6d","title":"虫师Selenium2+Python_6、Selenium IDE","link":"","description":"P155——创建测试用例\n\n\n\n\n\nP159——Selenium IDE 命令\n在浏览器中打开URL,可以接受相对路径和绝对路径两种形式\nopen\nopen(url)\n \n单击链接、按钮、复选框和单选框\nclick(elementLocator)\n \n模拟键盘的输入,向指定的input中输入值\ntype(inputLocator,value)\n \n根据optionSpecifier选项选择器来选择","image":"https://article.cdnof.com/2307/46eef30a-306f-4317-a82a-1dc3305011c8.png","keywords":["Selenium","IDE","self","driver","alert","selenium","element","Firefox","浏览器","脚本"],"created_at":"0001-01-01T00:00:00Z"}]},"__N_SSG":true},"page":"/article/[article_id]","query":{"article_id":"22721bca-b928-4959-add5-a2042074a7fe"},"buildId":"7EtL49Y65E8zx1NwcIC_o","isFallback":false,"gsp":true,"scriptLoader":[]}</script></body></html>