英文:
How to use last child as a parent in selenium javascript?
问题
<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)
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)
![enter image description here][1]
<details>
<summary>英文:</summary>
I want to get input element of last child, this snippet is code of last child. I want to get text `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>
Trying to get last child using
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)
It's scroll down to last child ```class="priority-intent-div">```. When trying to get text from the last child (text in input tag), using
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)
it given value of first child. Anyone can help? [![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/rBu72.png
</details>
# 答案1
**得分**: 0
```javascript
let intentList = await driver.findElements(By.xpath(`//div[@class='priority-intent-list']`));
let lastList = intentList[intentList.length - 1];
let intentDiv = await intentList.findElements(By.xpath(`//div[@class='priority-intent-div']`));
let lastIntentDiv = intentDiv[intentDiv.length - 1];
let answer = await lastIntentDiv.findElement(By.xpath(`.//input[@type='text']`)).getAttribute('value');
or
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 elements will give you a collection of elements, get the last one or use XPath to find the input directly, something like this `//input[(@value='Text Reply - Delete')]`
read this once: https://www.guru99.com/xpath-selenium.html
英文:
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')
or
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 elements will give you a collection of elements, get the last one or use XPath to find the input directly, something like this //input[(@value='Text Reply - Delete')]
read this once: https://www.guru99.com/xpath-selenium.html
*
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论