我正在尝试通过一组下拉字段实现自动化,这些字段将过滤票证数据库并导出 csv 以供进一步处理。其中一个字段是“所有者”对话框,人们可以在其中输入姓名,下拉菜单尝试从后端数据库中找出全名。我只用一个人的名字过滤。通过反复试验:我收到以下异常:
OpenQA.Selenium.ElementNotInteractableException:'元素不可交互(会话信息:chrome = 75.0.3770.100)'
使用 Selenium IDE 逐步浏览网站并记录每个步骤,导出会生成以下内容(在 Java 中):
driver.findElement(By.cssSelector(".col-xs-2:nth-child(7) .ui-select-search")).click();
driver.findElement(By.cssSelector(".ng-touched")).sendKeys("First Lastname");
driver.findElement(By.cssSelector(".ng-touched")).sendKeys(Keys.ENTER);
所以我在我的代码中使用了这些元素,(即使我无法在 HTML 代码块中找到它们):
var owner = driver.FindElement(By.CssSelector(".col-xs-2:nth-child(7)"));
owner.Click();
这样就成功点击了字段,我需要在字段中填一个名字。
owner.SendKeys("First Last");
这会产生错误。我尝试了各种 By.CssSelectors、By.Id、By.XPath,包括 By.XPath("*[@placeholder='Owner']") 并等待不同的时间(X)使用System.Threading.Thread.Sleep(X);
在进一步研究中,我尝试了以下代码:
elements = driver.FindElements(By.TagName("employee-search"));
foreach (IWebElement a in elements)
{
Console.WriteLine(a.ToString());
}
这产生:
Element (id = 54e057b8-2ca1-4d35-a73e-1cbdc068b50b)
Element (id = 4d0aae2b-fc01-4658-8585-54b415cb1c33)
Element (id = 1a94ce3f-ec5e-4c6b-aab2-4df26159d686)
如果我将 .ToString() 编辑为 .Text,如下所示:
elements = driver.FindElements(By.TagName("employee-search"));
foreach (IWebElement a in elements)
{
Console.WriteLine(a.Text);
}
控制台不产生任何输出。
这是我正在使用的 HTML 块:
<div class="ui-select-container selectize-control single ng-pristine ng-valid ng-empty ng-touched open" ng-class="{'open': $select.open}" ng-disabled="$ctrl.disabled" ng-model="$ctrl.employee.selected" theme="selectize" append-to-body="true" on-select="$ctrl.selectEmployee($item, $model)" skip-focusser="true" style="position: absolute; left: 1226.5px; top: 281px; width: 207.104px;">
<div class="selectize-input focus" ng-class="{'focus': $select.open, 'disabled': $select.disabled, 'selectize-focus' : $select.focus}" ng-click="$select.open && !$select.searchEnabled ? $select.toggle($event) : $select.activate()" style="">
<div ng-hide="$select.searchEnabled && ($select.open || $select.isEmpty())" class="ui-select-match ng-hide" placeholder="Owner">
<span ng-show="!$select.searchEnabled && ($select.isEmpty() || $select.open)" class="ui-select-placeholder text-muted ng-hide">Owner</span>
<span ng-hide="$select.isEmpty() || $select.open" ng-transclude="" class="ng-hide"></span>
</div>
<input type="search" autocomplete="off" tabindex="-1" class="ui-select-search ui-select-toggle ng-valid ng-touched ng-dirty ng-empty" ng-class="{'ui-select-search-hidden':!$select.searchEnabled}" ng-click="$select.toggle($event)" placeholder="Owner" ng-model="$select.search" ng-hide="!$select.isEmpty() && !$select.open" ng-disabled="$select.disabled" aria-label="Select box" style="width: 194px;">
</div>
我期望 SendKeys 语句输入名字和姓氏,然后在导出 CSV 之前将其添加到过滤器参数中。与网站交互允许在字段中单击、键盘输入,如果进行了肯定匹配,请按 Enter 锁定过滤器。相反,根据我正在查找的元素,它会告诉我它不可交互或未定位。我怀疑它需要JavascriptExecutor。当我假设我需要翻转标签时,这会是什么样子:
ng-show="!$select.searchEnabled"
启用它们(没有!)。如果其中有任何令人困惑、格式不正确、需要更多信息等,请发表评论以帮助我。非常感谢任何帮助。