in spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/tool/browser/InteractiveTextProcessor.java [252:325]
private String generateElementInfoString(int index, WebElement element, WebDriver driver) {
try {
// 使用JavaScript获取元素的详细信息
JavascriptExecutor js = (JavascriptExecutor) driver;
@SuppressWarnings("unchecked")
Map<String, Object> props = (Map<String, Object>) js.executeScript("""
function getElementInfo(el) {
try {
const style = window.getComputedStyle(el);
return {
tagName: el.tagName.toLowerCase(),
type: el.getAttribute('type'),
role: el.getAttribute('role'),
text: el.textContent.trim(),
value: el.value,
placeholder: el.getAttribute('placeholder'),
name: el.getAttribute('name'),
id: el.getAttribute('id'),
'aria-label': el.getAttribute('aria-label'),
isVisible: (
el.offsetWidth > 0 &&
el.offsetHeight > 0 &&
style.visibility !== 'hidden' &&
style.display !== 'none'
)
};
} catch(e) {
return null; // 如果获取元素信息失败,返回null
}
}
return getElementInfo(arguments[0]);
""", element);
if (props == null || !(Boolean) props.get("isVisible")) {
return "";
}
// 构建HTML属性字符串
StringBuilder attributes = new StringBuilder();
// 添加基本属性
if (props.get("type") != null) {
attributes.append(" type=\"").append(props.get("type")).append("\"");
}
if (props.get("role") != null) {
attributes.append(" role=\"").append(props.get("role")).append("\"");
}
if (props.get("placeholder") != null) {
attributes.append(" placeholder=\"").append(props.get("placeholder")).append("\"");
}
if (props.get("name") != null) {
attributes.append(" name=\"").append(props.get("name")).append("\"");
}
if (props.get("id") != null) {
attributes.append(" id=\"").append(props.get("id")).append("\"");
}
if (props.get("aria-label") != null) {
attributes.append(" aria-label=\"").append(props.get("aria-label")).append("\"");
}
if (props.get("value") != null) {
attributes.append(" value=\"").append(props.get("value")).append("\"");
}
String tagName = (String) props.get("tagName");
String text = (String) props.get("text");
// 生成标准HTML格式输出
return String.format("[%d] <%s%s>%s</%s>\n", index, tagName, attributes.toString(), text, tagName);
}
catch (Exception e) {
log.warn("生成元素信息字符串失败: {}", e.getMessage());
return "";
}
}