[英]How to select last element in Selenium Webdriver using Javascript?
I create a script to verify that an element is successfully added.我创建了一个脚本来验证元素是否已成功添加。 The added element always at the end of the page or become last element in different div.
添加的元素始终位于页面末尾或成为不同 div 中的最后一个元素。 I am using selenium webdriver and javascript.
我正在使用 selenium webdriver 和 javascript。 Here's HTML code
这是 HTML 代码
<div class="row">
<div class="form-group col-sm-2 col-xs-12">
<label>Entity Name</label>
<input value="ttax" class="form-control entity-name" disabled="">
</div>
</div>
<div class="row">
<div class="form-group col-sm-2 col-xs-12">
<label>Entity Name</label>
<input value="Room" class="form-control entity-name" disabled="">
</div>
</div>
I tried this我试过这个
let new_entity = await driver.findElement(By.css("div:nth-child(2) > div.form-group.col-sm-2.col-xs-12 > input").last()
let entity_value = await new_entity.getAttribute('value')
expect(entity_value).to.be.eq(entity_name)
But I got error element.last() is not function
但我收到错误
element.last() is not function
Can anyone help me?谁能帮我? I can not find references about selenium webdriver in javascript, most of them using java.
我在 javascript 中找不到关于 selenium webdriver 的参考,其中大部分使用 java。 ss
SS
Here when you want to use the last or first element then we have to get an element array.在这里,当您想使用最后一个或第一个元素时,我们必须获得一个元素数组。 Instead of findElement, we can use findElements
我们可以使用 findElements 代替 findElement
let new_entity = await driver.findElements(By.css("..")).last();
let entity_value = await new_entity.getAttribute('value'); let entity_value = await new_entity.getAttribute('value'); expect(entity_value).to.be.eq(entity_name);
期望(实体值)。to.be.eq(实体名称);
As the added element always becomes the last element to verify the added element you need to locate the last-child
of the parent element which you haven't shown us in the HTML within the question.由于添加的元素始终成为验证添加元素的最后一个元素,因此您需要找到父元素的
last-child
元素,您没有在问题的 HTML 中向我们显示该元素。 So to locate the last-child
you can use the following css-selectors based Locator Strategy :因此,要定位
last-child
您可以使用以下基于css-selectors的Locator Strategy :
Using css :使用css :
let new_entity = await driver.findElement(By.css("immediateParentTag div:last-child > div.form-group.col-sm-2.col-xs-12 > input"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.