Get Web Element by CSS Selector
Command description
Find a unique element in a webpage or any of its frames with a CSS selector. A successful match receives an ms-rpa-id in the page DOM, and the command returns a WebElement.
Use this command when a stable CSS selector is already known, for example from the page source, browser developer tools, or AI analysis of the webpage structure.
Command Input Parameters
| input parameters | Input parameter type | Description |
|---|---|---|
| Webpage Object | WebPage | Webpage in which to find the element |
| CSS Selector | str | CSS selector, such as #kw, .item[data-id="1"], input[name="q"] |
| Element Wait Duration | number | Maximum time to wait for exactly one matching element, in seconds |
Command Output Parameters
| Output parameters | Output parameter type | Description |
|---|---|---|
| Web Element | WebElement | Matching web element |
Description
- The selector is evaluated in the main page and all frames.
- CSS selectors must match unique elements.
- If there is no matching element, or multiple elements are matched, the search will continue until only one element is matched.
- If zero or multiple elements still match when Element Wait Duration expires, the command raises an exception. Refine the selector so it matches exactly one element.
- Use the returned
WebElementwith commands for clicking, input, reading properties, executing JavaScript, and other element operations. - Use standard browser CSS selector syntax; do not enter XPath.
Example
Find a search input on a webpage:
[
{
"children": [],
"in": {
"Browser Type": "'Google Chrome'",
"Match Method": "'Current Webpage'",
"Page Load Timeout": "30"
},
"out": {
"Webpage Object": "webPage"
},
"ins": "Get Open Webpage"
},
{
"children": [],
"in": {
"Webpage Object": "webPage",
"CSS Selector": "'#__docusaurus div.navbarSearchContainer_Bca1 > div > input'",
"Element Wait Duration": "10"
},
"out": {
"Web Element": "searchInput"
},
"ins": "Get Web Element by CSS Selector"
},
{
"children": [],
"in": {
"Webpage Object": "webPage",
"Web Input Element": "searchInput",
"Input Content": "'xxx'",
"Append Input": "False",
"Tab Key": "False",
"Enter Key": "False",
"Input Method": "'CDP Silent Input'",
"Click Element Before Input": "False",
"English Keyboard Layout": "False",
"Post-Input Delay": "1",
"Element Wait Duration": "30",
"Post-Click Delay": "1"
},
"out": {},
"comments": "Enter search content",
"ins": "Input into Web Element"
}
]