如何在Selenium JavaScript中将最后一个子元素用作父元素?

huangapple go评论95阅读模式
英文:

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&#39;s scroll down to last child ```class=&quot;priority-intent-div&quot;&gt;```. 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=&#39;priority-intent-list&#39;]`));
let lastList = intentList[intentList.lenght - 1];


let intentDiv = await intentList.findElements(By.xpath(`//div[@class=&#39;priority-intent-div&#39;]`));
let lastIntentDiv = intentDiv[intentDiv.lenght - 1]
let answer = await lastIntentDiv.findElement(By.xpath(`.//input[@type=&#39;text&#39;]`)).getAttribute(&#39;value&#39;)

or

let result = await driver.findelement(By.xpath(`//div[@class=&#39;priority-intent-list&#39;][last()] //div[@class=&#39;priority-intent-div&#39;][last()] //input[@type=&#39;text&#39;]`))
priority_intent_value = await result.getAttribute(&#39;value&#39;)

*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=&#39;Text Reply - Delete&#39;)]
read this once: https://www.guru99.com/xpath-selenium.html
*

huangapple
  • 本文由 发表于 2020年10月22日 10:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64474317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定