简体   繁体   English

如何使用 Javascript 选择 Selenium Webdriver 中的最后一个元素?

[英]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 based Locator Strategy :因此,要定位last-child您可以使用以下基于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.

相关问题 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? Selenium-WebDriver 如何使用 javascript 和 firefox 浏览器突出显示元素 - Selenium-WebDriver how to highlight element using javascript and firefox browser 如何使用Safari的JavaScript双击Selenium Webdriver上的元素 - How to double click an element on Selenium Webdriver using JavaScript for Safari 如何使用 Selenium WebDriver 检查元素是否进入视图? - How to check if an element is into view using Selenium WebDriver? 如何与使用Selenium Webdriver隐藏的元素进行交互? - How to interact with an element that is hidden using Selenium Webdriver? 如何使用Selenium Webdriver处理Javascript单击? - How to handle Javascript click by using selenium Webdriver? 如何使用webdriver(JavaScript)在selenium中停止测试? - How to stop tests in selenium using webdriver (JavaScript)? 如何使用Java从Selenium WebDriver中的不可见下拉元素中选择选项 - How to select option from invisible drop-down element in Selenium WebDriver using Java 如何使用javascript使用java使用selenium Webdriver设置所选Web元素的属性? - How to use javascript to set attribute of selected web element using selenium Webdriver using java? 使用Selenium WebDriver和JavaScript从XPath找到Element的文本 - Get text from XPath located Element using Selenium WebDriver with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM