1

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. I am using selenium webdriver and javascript. Here's HTML code

<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

Can anyone help me? I can not find references about selenium webdriver in javascript, most of them using java. ss

1

2 Answers 2

0

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. So to locate the last-child you can use the following based Locator Strategy:

  • Using css:

    let new_entity = await driver.findElement(By.css("immediateParentTag div:last-child > div.form-group.col-sm-2.col-xs-12 > input"))
    
Sign up to request clarification or add additional context in comments.

6 Comments

I still can not get the last element, I got first element, I want to get 'Room'. I attached the snippet code on my post
Instead of the image can you update the question with the text based HTML?
You need to provide the HTML of the first element which you don't want so we can adjust the locator, preferably along with the parent element.
html of 1st element added
solved using let entity_last_block = await driver.findElement(By.css('div.card.card-entity:last-child')) as parent. Thanks, debanjanB
|
0

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

let new_entity = await driver.findElements(By.css("..")).last();

let entity_value = await new_entity.getAttribute('value'); expect(entity_value).to.be.eq(entity_name);

1 Comment

I tried using yours but got error InvalidSelectorError: invalid selector: An invalid or illegal selector was specified

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.