MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / TargetLocator

Class TargetLocator

javascript/selenium-webdriver/lib/webdriver.js:2408–2541  ·  view source on GitHub ↗

* An interface for changing the focus of the driver to another frame or window. * * This class should never be instantiated directly. Instead, obtain an * instance with * * webdriver.switchTo() * * @see WebDriver#switchTo()

Source from the content-addressed store, hash-verified

2406 * @see WebDriver#switchTo()
2407 */
2408class TargetLocator {
2409 /**
2410 * @param {!WebDriver} driver The parent driver.
2411 * @private
2412 */
2413 constructor(driver) {
2414 /** @private {!WebDriver} */
2415 this.driver_ = driver
2416 }
2417
2418 /**
2419 * Locates the DOM element on the current page that corresponds to
2420 * `document.activeElement` or `document.body` if the active element is not
2421 * available.
2422 *
2423 * @return {!WebElementPromise} The active element.
2424 */
2425 activeElement() {
2426 const id = this.driver_.execute(new command.Command(command.Name.GET_ACTIVE_ELEMENT))
2427 return new WebElementPromise(this.driver_, id)
2428 }
2429
2430 /**
2431 * Switches focus of all future commands to the topmost frame in the current
2432 * window.
2433 *
2434 * @return {!Promise<void>} A promise that will be resolved
2435 * when the driver has changed focus to the default content.
2436 */
2437 defaultContent() {
2438 return this.driver_.execute(new command.Command(command.Name.SWITCH_TO_FRAME).setParameter('id', null))
2439 }
2440
2441 /**
2442 * Changes the focus of all future commands to another frame on the page. The
2443 * target frame may be specified as one of the following:
2444 *
2445 * - A number that specifies a (zero-based) index into [window.frames](
2446 * https://developer.mozilla.org/en-US/docs/Web/API/Window.frames).
2447 * - A {@link WebElement} reference, which correspond to a `frame` or `iframe`
2448 * DOM element.
2449 * - The `null` value, to select the topmost frame on the page. Passing `null`
2450 * is the same as calling {@link #defaultContent defaultContent()}.
2451 *
2452 * If the specified frame can not be found, the returned promise will be
2453 * rejected with a {@linkplain error.NoSuchFrameError}.
2454 *
2455 * @param {(number|string|WebElement|null)} id The frame locator.
2456 * @return {!Promise<void>} A promise that will be resolved
2457 * when the driver has changed focus to the specified frame.
2458 */
2459 frame(id) {
2460 let frameReference = id
2461 if (typeof id === 'string') {
2462 frameReference = this.driver_.findElement({ id }).catch((_) => this.driver_.findElement({ name: id }))
2463 }
2464
2465 return this.driver_.execute(new command.Command(command.Name.SWITCH_TO_FRAME).setParameter('id', frameReference))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected