Sets input focus on a specified UI element in a Windows application. This endpoint uses Windows UI Automation to directly interact with UI elements, providing a reliable way to set focus on interface elements before performing other actions.
This endpoint is useful when you need to ensure a specific element has focus before interacting with it, particularly for complex UI workflows.
{
"elementId": "string" // UI Automation element ID to set focus on
}
| Parameter | Type | Required | Description |
|---|---|---|---|
| elementId | string | Yes | The ID of the UI element to set focus on, as found in the UI-Overview-Json or obtained from the Get Automation Details endpoint. |
{
"message": "string", // Result message (e.g., "Element focused")
"success": boolean // Whether the operation was successful
}
| Field | Type | Description |
|---|---|---|
| message | string | Descriptive message about the result of the operation, such as "Element focused" or an error message if unsuccessful. |
| success | boolean | Indicates if the operation completed successfully (true) or not (false). |
import requests
import json
def set_focus_on_element(element_id, api_key):
url = "http://localhost:54321/tools-api/automation/set-focus"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"elementId": element_id
}
response = requests.post(url, headers=headers, json=data)
return response.json()
# Example usage
api_key = "your_api_key_here"
element_id = "AutomationId:textBox1|ClassName:TextBox"
result = set_focus_on_element(element_id, api_key)
print(result)
import axios from 'axios';
async function setFocusOnElement(elementId: string, apiKey: string) {
const url = 'http://localhost:54321/tools-api/automation/set-focus';
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
};
const data = {
elementId: elementId
};
try {
const response = await axios.post(url, data, { headers });
return response.data;
} catch (error) {
console.error('Error setting focus:', error);
throw error;
}
}
// Example usage
const apiKey = 'your_api_key_here';
const elementId = 'AutomationId:textBox1|ClassName:TextBox';
setFocusOnElement(elementId, apiKey)
.then(result => console.log(result))
.catch(error => console.error(error));
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class ToolsServerClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
public ToolsServerClient(string apiKey, string baseUrl = "http://localhost:54321")
{
_httpClient = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
_apiKey = apiKey;
}
public async Task SetFocusAsync(string elementId)
{
var request = new AutomationSetFocusRequest
{
ElementId = elementId
};
return await SendRequestAsync(
"tools-api/automation/set-focus",
request
);
}
private async Task SendRequestAsync(string endpoint, TRequest requestData)
{
var json = JsonSerializer.Serialize(requestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");
_httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _apiKey);
var response = await _httpClient.PostAsync(endpoint, content);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize(responseJson);
}
}
// Models
public class AutomationSetFocusRequest
{
public string ElementId { get; set; }
}
public class SimpleResponse
{
public string Message { get; set; }
public bool Success { get; set; }
}
// Example usage
class Program
{
static async Task Main()
{
var client = new ToolsServerClient("your_api_key_here");
var elementId = "AutomationId:textBox1|ClassName:TextBox";
try
{
var result = await client.SetFocusAsync(elementId);
Console.WriteLine($"Success: {result.Success}, Message: {result.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
This example demonstrates setting focus on a text box element in a Windows application:
// Request
POST /tools-api/automation/set-focus
{
"elementId": "AutomationId:textBox1|ClassName:TextBox"
}
// Response
{
"message": "Element focused",
"success": true
}
Setting focus on interactive elements like buttons before clicking them can be useful for more reliable interactions:
// Request
POST /tools-api/automation/set-focus
{
"elementId": "AutomationId:btnSubmit|ClassName:Button"
}
// Response
{
"message": "Element focused",
"success": true
}