在我的代码中,我通常可以通过Xpath找到一个元素并执行如下所示的操作
await I.fillField('//*[@id="edit-name"]','user1');
当我执行以下操作时,我看到以下错误。由于 ID是动态创建的。有没有建议的方法来解决此类元素?
await I.fillField('//*[@id="crmUiId_1"]','SomeTextHere');
错误:
**TypeError: Cannot read property '$$' of null
at findElements (node_modules/codeceptjs/lib/helper/Playwright.js:2087:18)
at Playwright._locate (node_modules/codeceptjs/lib/helper/Playwright.js:822:12)**
关联的HTML:
<div crm-ui-field="{name: 'caseTypeDetailForm.title', title: ts('Title')}" class="ng-isolate-scope crm-section"><div class="label">
<label crm-ui-for="caseTypeDetailForm.title" crm-depth="1" crm-ui-force-required="" for="crmUiId_1"><span ng-class="cssClasses"><span ng-transclude=""><span class="ng-binding ng-scope">Title</span></span><span crm-ui-visible="crmIsRequired" class="crm-marker ng-isolate-scope" title="This field is required." style="visibility: inherit;">*</span></span></label>
<!-- ngIf: help -->
</div>
<div class="content" ng-transclude="">
<input crm-ui-id="caseTypeDetailForm.title" type="text" name="title" ng-model="caseType.title" class="big crm-form-text ng-pristine ng-scope ng-empty ng-invalid ng-invalid-required ng-touched" required="" id="crmUiId_1">
</div>
<div class="clear"></div>
</div>
这里的问题实际上是ID“ crmUiId_1”是动态生成的。相反,我尝试使用ng-model =“ caseType.title”尝试xpath,但它似乎也不起作用。
答案 0 :(得分:0)
我只是确保您等待它:
await page.waitForSelector('#crmUiId_1')
await page.fill('#crmUiId_1', 'whatever')
否则页面可能仍在加载。