我想提取文本,然后使用 UiPath 单击 Google Chrome JavaScript 警报弹出窗口中的按钮。
具体来说,我遇到了以下问题:
- 定位对话框的选择器是什么?如何找到它?
- 从对话框中定位和提取文本的选择器是什么?如何找到它?
- 点击对话框中“确定”按钮的选择器是什么?如何找到它?
我知道对于 Web 自动化,UiPath 建议在大多数情况下使用 IE,因为 UiPath 可以使用其浏览器 COM 对象以更“ native ”的方式与之交互。其他浏览器会有限制,并且在版本更新时可能会遇到困难。但是,在某些情况下,无法使用 IE,在我的工作中,我们与 Google 签订了工作协议(protocol),并且我们的许多内部网站无法在 IE 中运行。
要完成 UiPath 3 级高级认证(在线):作业 2 - 生成年度报告,您必须创建一个机器人来执行以下操作:
- 导航至https://acme-test.uipath.com/
- 使用用户名和密码登录
- 点击各种元素即可在系统中导航
- 与 JavaScript 警报弹出窗口进行交互并提取数据
问题是 UiExplorer 无法找到正确的选择器,这使得从 Google Chrome 弹出窗口获取文本变得困难,因为它的渲染方式与 IE 不同,并且不公开文本属性(其中其他的)。这是来自 acme-test 站点的一个片段,用于创建警报以帮助定义选择器,这是一个 UiPath XAML file :
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
<title>Upload reports</title>
</head>
<body>
<!-- Page Content -->
<div class="container">
<button type="button" class="btn btn-primary" id="buttonUpload">Upload Report</button>
<script type="text/javascript">
jQuery(document).ready(function() {
$('input[type=file]').on('change', prepareUpload);
function prepareUpload(event) {
files = event.target.files;
$('#upload-file-info').html(files[0].name)
}
jQuery('#buttonUpload').click(function(event) {
event.stopPropagation(); // Stop stuff happening
event.preventDefault(); // Totally stop stuff happening
var vTaxID = jQuery('#vendorTaxID').val();
var rYear = jQuery('#reportYear').val();
// If nothing is filled
if (vTaxID == "" || rYear == "") {
alert('Plase fill in the Vendor TaxID and Report Year!');
return;
}
if (typeof files === 'undefined') {
alert('Please select the Report File');
return;
}
// Create a formdata object and add the files
var data = new FormData();
jQuery.each(files, function(key, value) {
data.append(key, value);
});
console.log('...');
jQuery.ajax({
url: "/cmajax.php?cmd=uploadReport&cvTaxID=" + vTaxID + "&crYear=" + rYear,
type: "POST",
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
})
.always(function(msg) {
console.log('done');
if (msg.responseText == 'bad')
alert("Some problems were encountered.");
else {
console.log(msg);
alert('Report was uploaded - confirmation id is ' + msg.responseText);
}
});
})
});
</script>
</div>
</body>
</html>
最佳答案
以下内容可能并不完全是您正在寻找的内容,但它可能提供替代解决方案。在大多数 RPA 工具中,警报的自动化可能有点麻烦,因此我喜欢使用警报覆盖技巧。
基本上,您注入(inject)一个函数,该函数将覆盖默认警报函数(显示弹出窗口)并将其内容重新路由到 UiPath 可访问的某个位置,例如自定义文本框。
更详细地说,您在某处创建一个文本框...
var body = document.getElementsByTagName("body")[0];
var text = document.createElement("input");
text.id = "alertOutput";
body.insertBefore(text, body.firstChild);
...然后通过消息重新路由覆盖警报功能。
function alert(message) {
var text = document.getElementById("alertOutput");
text.value = message; // or .innerText
}
现在,页面上的每个警报都会将其内容直接发送到文本框,而不是显示弹出窗口。
PS:我故意不在我的代码中使用 jQuery
,但看到它在您的网站上可用,您可以利用它。
关于javascript - 如何使用 UiPath 从 Google Chrome JavaScript 警报弹出窗口中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617056/