Get Web Element by XPath
Command description
Find a unique element in a webpage or any of its frames with XPath. A successful match receives an ms-rpa-id in the page DOM, and the command returns a WebElement.
Use this command when a stable XPath 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 |
| XPath | str | XPath expression, such as //input[@name="q"] or //*[@id="kw"] |
| 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 XPath is evaluated in the main page and all frames.
- XPath must match element nodes and cannot use expressions that return text, attributes, or other non-element nodes, for example, do not write
//a/text()or//a/@href. - The XPath must ultimately match a unique element.
- 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 XPath 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 XPath syntax; do not enter a CSS selector.
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",
"XPath": "'//*[@id=\"__docusaurus\"]/nav[1]/div[1]/div[2]/div[2]/div/input'",
"Element Wait Duration": "10"
},
"out": {
"Web Element": "searchInput"
},
"comments": "Get the search input box through XPath",
"ins": "Get Web Element by XPath"
},
{
"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": {},
"ins": "Input into Web Element"
}
]