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

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

How to use last child as a parent in selenium javascript?

问题

  1. <div class="priority-intent-div">
  2. <div class="row add-priority-intent-div">
  3. <div class="col-xs-12">
  4. <input type="hidden" name="priorityIntentId" value="5f8fb2f031f8a20a0cd6e2d1">
  5. <div class="form-group">
  6. <div class="intent-conditions">
  7. <button onclick="addIntentCondition(this)" type="button" class="btn btn-default" style="width: 100%">
  8. <span class="fa fa-plus"></span> Add Condition</button>
  9. </div>
  10. <label for="priorityIntentName" class="control-label">Intent Name</label>
  11. <input type="text" name="priorityIntentName" placeholder="Greeting" single="" maxlength="20" class="form-control maxlengthBadge" value="Text Reply - Delete" required="">
  12. <div class="help-block with-errors"></div>
  1. let block = await driver.findElement(By.css('div.priority-list-tab > div.priority-intent-list > div:last-child'))
  2. await driver.executeScript("arguments[0].scrollIntoView(true)", block)
  1. priority_intent_name = await block.findElement(By.xpath('div/div[1]/div/input'))
  2. priority_intent_value = await priority_intent_name.getAttribute('value')
  3. expect(priority_intent_value).to.be.eq(intent_name)

![enter image description here][1]

  1. <details>
  2. <summary>英文:</summary>
  3. 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>

  1. 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)

  1. 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)

  1. it given value of first child. Anyone can help? [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/rBu72.png
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. ```javascript
  7. let intentList = await driver.findElements(By.xpath(`//div[@class='priority-intent-list']`));
  8. let lastList = intentList[intentList.length - 1];
  9. let intentDiv = await intentList.findElements(By.xpath(`//div[@class='priority-intent-div']`));
  10. let lastIntentDiv = intentDiv[intentDiv.length - 1];
  11. let answer = await lastIntentDiv.findElement(By.xpath(`.//input[@type='text']`)).getAttribute('value');
  12. or
  13. let result = await driver.findElement(By.xpath(`//div[@class='priority-intent-list'][last()] //div[@class='priority-intent-div'][last()] //input[@type='text']`));
  14. priority_intent_value = await result.getAttribute('value');
  15. *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')]`
  16. read this once: https://www.guru99.com/xpath-selenium.html
英文:
  1. let intentList = await driver.findElements(By.xpath(`//div[@class=&#39;priority-intent-list&#39;]`));
  2. let lastList = intentList[intentList.lenght - 1];
  3. let intentDiv = await intentList.findElements(By.xpath(`//div[@class=&#39;priority-intent-div&#39;]`));
  4. let lastIntentDiv = intentDiv[intentDiv.lenght - 1]
  5. let answer = await lastIntentDiv.findElement(By.xpath(`.//input[@type=&#39;text&#39;]`)).getAttribute(&#39;value&#39;)

or

  1. 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;]`))
  2. 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:

确定