1

How do i find the last element in dynamic list with images that all have the same id is there a function that i can use like

driver.find_element_by_id('editwebsite').last()

in python, I want to select the last image in a table that have images with the same name

  • do you know what should be the number of the elements in the list? – Alexey R. Mar 29 '18 at 9:52
  • No as this changes dynamically as rows are added – Rizwen Butt Mar 29 '18 at 10:09
  • Then there is no way to get the last one. If you do not know what element should be the last one, how would Selenium know that? – Alexey R. Mar 29 '18 at 10:09
  • I was wondering if there was a function similar to last() on c# which finds the last element with the matching id – Rizwen Butt Mar 29 '18 at 10:14
  • I have been able to find the number of the element by using the following – Rizwen Butt Mar 29 '18 at 13:02
2

The selenium method driver.find_elements_by_id() (note the plural form) returns a list of elements - even if there is a single one.
The idiomatic way of accessing list members is to use [] - [0] will return the 1st, [1] the 2nd, and so on; now, python also supports negative indices in the access - what they mean is "start from the back".

So for a list variable lst, accessing lst[-1] will get you the last element, lst[-2] the second to last, and so on.

In your particular case, this would be the usage:

driver.find_elements_by_id('editwebsite')[-1]

In languages that do not support negative indices, one would get the length of the list, and access the element at that position minus one (in most languages the list/arrays are zero based, e.g. the first element is at position 0). In python that would be:

elements = driver.find_elements_by_id('editwebsite')
size = len(elements)
target_element = elements[size -1]

, but in python that's not necessary (and some might consider it bad style, "unpythonic")

  • 1
    Thanks for all the help, this is the line of code i will use driver.find_elements_by_xpath('//*[@id="editWebsite"]')[-1].click() – Rizwen Butt Apr 3 '18 at 13:15
1

At first find all the images with the id. Then select the last image from the list. In python that code would be:

def get_last_image():
    img_list = driver.find_elements_by_id('editwebsite')
    return img_list[len(img_list)-1]

Note: list starts from zero in python. So the last element of list will be at length-1.

Important: Since your images is loading dynamically, to get the last image you should call get_last_image() every time. By doing that you can get the last image from updated image list every time.

  • 1
    Don't use list for a variable name - that's a language keyword you now override, and can't access any more (list() is an object constructor, to return, well, lists :) ) – Todor Minakov Mar 31 '18 at 8:42
0

Find a commonality in all the elements... and Get all the elements using xpath.

List<WebElement> image_list = driver.findElements(...);

This returns a list of webElements. You can get the last elemement by

WebElement last_element = image_list.get(image_list.size());

Now you can take control of the last_element. This will also take care of the dynamically loaded images. Hope this helps.

  • image_list.get(image_list.size()) will fail with an IndexOutOfBoundsException. List indexing in Java ranges between 0 and list.size() - 1. – hoefling Mar 31 '18 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.