如何解决如何在Selenium JavaScript中使用最后一个孩子作为父母?
我想获取最后一个孩子的输入元素,此代码段是最后一个孩子的代码。我想获取文本Text Reply - Delete
。
<div class="priority-intent-div">
<div class="row add-priority-intent-div">
<div class="col-xs-12">
<input type="hidden" name="priorityIntentId" value="5f8fb2f031f8a20a0cd6e2d1">
<div class="form-group">
<div class="intent-conditions">
<button onclick="addIntentCondition(this)" type="button" class="btn btn-default" style="width: 100%">
<span class="fa fa-plus"></span> Add Condition</button>
</div>
<label for="priorityIntentName" class="control-label">Intent Name</label>
<input type="text" name="priorityIntentName" placeholder="Greeting" single="" maxlength="20" class="form-control maxlengthBadge" value="Text Reply - Delete" required="">
<div class="help-block with-errors"></div>
试图让最后一个孩子使用
let block = await driver.findElement(By.css('div.priority-list-tab > div.priority-intent-list > div:last-child'))
await driver.executeScript("arguments[0].scrollIntoView(true)",block)
它向下滚动到最后一个孩子class="priority-intent-div">
。尝试从最后一个孩子(输入标签中的文本)获取文本时,请使用
priority_intent_name = await block.findElement(By.xpath('div/div[1]/div/input'))
priority_intent_value = await priority_intent_name.getAttribute('value')
expect(priority_intent_value).to.be.eq(intent_name)
解决方法
let intentList = await driver.findElements(By.xpath(`//div[@class='priority-intent-list']`));
let lastList = intentList[intentList.lenght - 1];
let intentDiv = await intentList.findElements(By.xpath(`//div[@class='priority-intent-div']`));
let lastIntentDiv = intentDiv[intentDiv.lenght - 1]
let answer = await lastIntentDiv.findElement(By.xpath(`.//input[@type='text']`)).getAttribute('value')
或
let result = await driver.findelement(By.xpath(`//div[@class='priority-intent-list'][last()] //div[@class='priority-intent-div'][last()] //input[@type='text']`))
priority_intent_value = await result.getAttribute('value')
* find元素将为您提供元素的集合,获取最后一个元素或使用XPath直接查找输入,例如//input[(@value='Text Reply - Delete')]
请阅读一次:https://www.guru99.com/xpath-selenium.html
*
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。