|
| 1 | +--- |
| 2 | +slug: _dollar |
| 3 | +sidebar_position: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +import Admonition from "@theme/Admonition"; |
| 7 | + |
| 8 | +# $ |
| 9 | + |
| 10 | +## Overview {#overview} |
| 11 | + |
| 12 | +Use the `$` command instead of [findElement][find-element] as a shorter command to get a single element on the page. |
| 13 | + |
| 14 | +The `$` command returns an object describing the element, on which you can call action commands without passing the selector. However, if you do pass a selector, it will first find the corresponding element and then call the action for that element. You can also pass an object as the selector, where the object contains the property `element-6066-11e4-a52e-4f735466cecf` with the value of the element reference. The command then converts the reference into an extended WebdriverIO element. Note: Use these element objects only if you are certain they exist on the page. This can be verified, for example, using the [isExisting](../isExisting) command. |
| 15 | + |
| 16 | +You can chain `$` or `$$` together to navigate down the DOM tree. But note that chaining `$` and `$$` commands makes sense only when using multiple selector strategies. Otherwise, you will be making unnecessary requests that will slow down the test (e.g., `$('body').$('div')` creates two requests, whereas `$('body div')` does the same in one request). |
| 17 | + |
| 18 | +<Admonition type="info"> |
| 19 | + Read more about how to select specific elements in the recipe "[How to use |
| 20 | + selectors][how-to-use-selectors]". |
| 21 | +</Admonition> |
| 22 | + |
| 23 | +## Usage {#usage} |
| 24 | + |
| 25 | +```javascript |
| 26 | +await browser.$(selector); |
| 27 | +``` |
| 28 | + |
| 29 | +## Command Parameters {#parameters} |
| 30 | + |
| 31 | +<table> |
| 32 | +<thead> |
| 33 | +<tr><td>**Name**</td><td>**Type**</td><td>**Description**</td></tr> |
| 34 | +</thead> |
| 35 | +<tbody> |
| 36 | +<tr><td>selector</td><td>String or Function or Matcher</td><td>Selector, JS function, or Matcher object to get a specific element.</td></tr> |
| 37 | + |
| 38 | +</tbody> |
| 39 | +</table> |
| 40 | + |
| 41 | +## Usage Examples {#examples} |
| 42 | + |
| 43 | +**index.html** |
| 44 | + |
| 45 | +```html |
| 46 | +<ul id="menu"> |
| 47 | + <li> |
| 48 | + <a href="/">Home</a> |
| 49 | + </li> |
| 50 | + <li> |
| 51 | + <a href="/">Developer Guide</a> |
| 52 | + </li> |
| 53 | + <li> |
| 54 | + <a href="/">API</a> |
| 55 | + </li> |
| 56 | + <li> |
| 57 | + <a href="/">Contribute</a> |
| 58 | + </li> |
| 59 | +</ul> |
| 60 | +``` |
| 61 | + |
| 62 | +```javascript |
| 63 | +it("should get text of a menu link - JS Function", async ({ browser }) => { |
| 64 | + const text = await browser.$("#menu"); |
| 65 | + |
| 66 | + console.log( |
| 67 | + await text |
| 68 | + .$$("li")[2] |
| 69 | + .$(function () { |
| 70 | + // Arrow function cannot be used here. |
| 71 | + // This is Element – https://developer.mozilla.org/en-US/docs/Web/API/Element |
| 72 | + // in this specific example – this is HTMLLIElement |
| 73 | + // |
| 74 | + // TypeScript users can do something like: |
| 75 | + // return (this as Element).querySelector('a') |
| 76 | + return this.querySelector("a"); // Element |
| 77 | + }) |
| 78 | + .getText(), |
| 79 | + ); // outputs: "API" |
| 80 | +}); |
| 81 | + |
| 82 | +it("should get text of a menu link", async ({ browser }) => { |
| 83 | + const text = await browser.$("#menu"); |
| 84 | + |
| 85 | + console.log(await text.$$("li")[2].$("a").getText()); // outputs: "API" |
| 86 | +}); |
| 87 | + |
| 88 | +it("should allow to convert protocol result of an element into a WebdriverIO element", async ({ |
| 89 | + browser, |
| 90 | +}) => { |
| 91 | + const activeElement = await browser.getActiveElement(); |
| 92 | + |
| 93 | + console.log(await browser.$(activeElement).getTagName()); // outputs the active element |
| 94 | +}); |
| 95 | + |
| 96 | +it("should use Android's DataMatcher or ViewMatcher selector", async ({ browser }) => { |
| 97 | + const menuItem = await browser.$({ |
| 98 | + name: "hasEntry", |
| 99 | + args: ["title", "ViewTitle"], |
| 100 | + class: "androidx.test.espresso.matcher.ViewMatchers", |
| 101 | + }); |
| 102 | + await menuItem.click(); |
| 103 | + |
| 104 | + const menuItem = await browser.$({ |
| 105 | + name: "hasEntry", |
| 106 | + args: ["title", "ViewTitle"], |
| 107 | + }); |
| 108 | + await menuItem.click(); |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +## Related Commands {#related} |
| 113 | + |
| 114 | +- [element.$$](../_dollardollar) |
| 115 | +- [browser.$](../../browser/_dollar) |
| 116 | +- [browser.$$](../../browser/_dollardollar) |
| 117 | + |
| 118 | +## References |
| 119 | + |
| 120 | +We'd like to give credit to the original WebdriverIO docs [article](https://webdriver.io/docs/api/element/$), from which we drew some information while writing our version. |
| 121 | + |
| 122 | +[find-element]: https://webdriver.io/docs/api/webdriver/#findelement |
| 123 | +[how-to-use-selectors]: https://webdriver.io/docs/selectors |
0 commit comments