How to ALWAYS select the last item in a DYNAMIC list

2,451 views
Skip to first unread message

Raj Lakhanpal

unread,
Feb 10, 2015, 11:03:42 AM2/10/15
to seleniu...@googlegroups.com
I need to Always get the list item in a Dynamic list. Can anyone please suggest how can I achieve this?

Thanks for the help.

Selenium Framework

unread,
Feb 10, 2015, 11:13:37 AM2/10/15
to seleniu...@googlegroups.com
Java
List<WebElement> mylist = new Select(driver.findElement(By.xxx)));
mylist
.get(mylist.size-1) //returns the last WebElement always


Ruby (using watir)
all_options = browser.select_list(.....).options
all_options
.last # returns last option


cheers,

PeterJeffreyGale

unread,
Feb 10, 2015, 11:36:59 AM2/10/15
to seleniu...@googlegroups.com
If you are using XPath, which I owudl recommend, you can achieve it with just the findElement() line.

For example, you you are searching the list of all elements: //*

then you can find just the last element in this list like this: (//*)[last()]

anemuday

unread,
Feb 10, 2015, 11:48:25 AM2/10/15
to seleniu...@googlegroups.com
Raj,

It is all depends on how your listbox is displayed.

Is the listbox is "select" with "options" has to be handled differently with many "ul" with "li" items.

So let us know which object you have in your application. It can be great if you can provide a sample html/screenshot.

Thanks,
Uday
Selenium WebDriver Videos

Raj Lakhanpal

unread,
Feb 10, 2015, 12:10:15 PM2/10/15
to seleniu...@googlegroups.com


On Tuesday, February 10, 2015 at 8:48:25 AM UTC-8, anemuday wrote:
Raj,

It is all depends on how your listbox is displayed.

Is the listbox is "select" with "options" has to be handled differently with many "ul" with "li" items.

So let us know which object you have in your application. It can be great if you can provide a sample html/screenshot.

Thanks,
Uday


HTML:
<div id="overview_form">
<ol>

<li>
<span>1.</span>
<label class="input_label" for="sites">Sites*</label>
<div class="select2-container select2-container-active select2-dropdown-    open" id="s2id_autogen1" style="width: 500px;">
<a tabindex="-1" class="select2-choice" onclick="return false;"     href="javascript:void(0)">   
<span>www.roger.com_20150210075155</span>
<abbr style="display:none;" class="select2-search-choice-close"></abbr>   
<div><b></b></div></a>
<input type="text" class="select2-focusser select2-offscreen" disabled="disabled">
</div>
<select style=" size="1" name="siteKey" class="select2-offscreen" tabindex="-1">

<option value="30518">www.roger.com_20150209191817</option>
<option value="30520">www.roger.com_20150209192123</option>
<option value="30522">www.roger.com_20150209192351</option>
<option value="30524">www.roger.com_20150209192910</option>
<option value="30528">www.roger.com_20150209193425</option>
<option value="30529">www.roger.com_20150209193801</option>
<option value="30531">www.roger.com_20150209194009</option>
<option value="30546">www.roger.com_20150210074133</option>
<option value="30548">www.roger.com_20150210074359</option>
<option value="30550">www.roger.com_20150210075155</option></select>

</li>
</ol> 
</div> 

Raj Lakhanpal

unread,
Feb 10, 2015, 12:11:05 PM2/10/15
to seleniu...@googlegroups.com
I think your java structure is not correct!

PeterJeffreyGale

unread,
Feb 10, 2015, 12:24:37 PM2/10/15
to seleniu...@googlegroups.com
Is the text for all those items actually the same???

Assuming they are different, which they should be, I would recommend using XPath to identify the last item in the list and retrieve the text value for that selection , and then running a standard select command for the item text so retrieved,

Raj Lakhanpal

unread,
Feb 10, 2015, 12:32:20 PM2/10/15
to seleniu...@googlegroups.com


On Tuesday, February 10, 2015 at 9:24:37 AM UTC-8, PeterJeffreyGale wrote:
Is the text for all those items actually the same???

Assuming they are different, which they should be, I would recommend using XPath to identify the last item in the list and retrieve the text value for that selection , and then running a standard select command for the item text so retrieved,

This is what I tried so far, but getting an error that "Cannot locate option with index: 123

WebElement selectElement = driver.findElement(By.name("siteKey"));

Select select = new Select();

select.selectByIndex(select.getOptions().size()); 

Anand S.

unread,
Feb 10, 2015, 1:39:44 PM2/10/15
to seleniu...@googlegroups.com

I tried below way, and it's working.. Maybe this will help u .

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.echoecho.com/htmlforms11.htm");
    Thread.sleep(100l);
    driver.findElement(By.xpath("//select[@name='dropdownmenu']")).click();
    Thread.sleep(100l);
    WebElement element = driver.findElement(By.xpath("//select[@name='dropdownmenu']"));
    List<WebElement> elements = element.findElements(By.tagName("option"));
    System.out.println(elements.get(elements.size() - 1).getText());

On Tuesday, 10 February 2015 21:33:42 UTC+5:30, Raj Lakhanpal wrote:

PeterJeffreyGale

unread,
Feb 10, 2015, 2:07:07 PM2/10/15
to seleniu...@googlegroups.com
Take 1 away as it's a zero based array.

Raj Lakhanpal

unread,
Feb 10, 2015, 2:42:17 PM2/10/15
to seleniu...@googlegroups.com
Tried that, i get an error "Element is not currently visible". Then, I tried this, and gave me an error that "Can not locate an element by index: 123

Select select = new Select(driver.findElement(By.id("siteKey")));
List<WebElement> l = select.getOptions(); 
int numElements = l.size();
select.selectByIndex(numElements);

PeterJeffreyGale

unread,
Feb 10, 2015, 2:50:07 PM2/10/15
to seleniu...@googlegroups.com
Your HTML doesn't show 123 (or even 122) items. What value are you expecting?

Raj Lakhanpal

unread,
Feb 10, 2015, 2:52:17 PM2/10/15
to seleniu...@googlegroups.com
Sorry about that,.Yes, it does not show. I just got that index from the list...right-clicked on the option and show xpath. It showed me the index. That was just an example of one of the option's index.

PeterJeffreyGale

unread,
Feb 10, 2015, 3:32:17 PM2/10/15
to seleniu...@googlegroups.com
And what value are you expecting?

Raj Lakhanpal

unread,
Feb 10, 2015, 3:41:29 PM2/10/15
to seleniu...@googlegroups.com
I need to select the last item in the list. So, here how it goes...An id is entered on a different page, then the page re-directs to another page, where I need to verify that the id entered in the previous step exists. If it exists, select it, and move to the next step...

PeterJeffreyGale

unread,
Feb 10, 2015, 3:57:34 PM2/10/15
to seleniu...@googlegroups.com
So what's your problem that you wnat help with?

Raj Lakhanpal

unread,
Feb 10, 2015, 4:01:44 PM2/10/15
to seleniu...@googlegroups.com
How to select the last item in the list

PeterJeffreyGale

unread,
Feb 10, 2015, 4:04:12 PM2/10/15
to seleniu...@googlegroups.com
>How to select the last item in the list

So in the example where you got a list size of 123, what value are you expecting to get?

Raj Lakhanpal

unread,
Feb 10, 2015, 4:22:08 PM2/10/15
to seleniu...@googlegroups.com
I want to select that item, because that is the last item

PeterJeffreyGale

unread,
Feb 10, 2015, 4:26:29 PM2/10/15
to seleniu...@googlegroups.com
Sorry, I obviously can't help you.
Reply all
Reply to author
Forward
0 new messages