How to click second element from List

417 views
Skip to first unread message

Vinay

unread,
Jul 1, 2014, 12:48:18 AM7/1/14
to webd...@googlegroups.com

List<WebElement> elements = driver.findElements(By.className("Submit-button-icon"));

Thread.sleep(1000);

System.out.println("Test number of elements: " + elements.size());

WebElement button=elements.get(0);

      button.click();

I need to click second element using "elements" array

I used this:

WebElement button1=elements.get(1);

      button1.click();

But this is not working.Please help


Andreas Tolfsen

unread,
Jul 1, 2014, 6:51:29 AM7/1/14
to webd...@googlegroups.com
On 01/07/2014 05:48, Vinay wrote:
> List<WebElement> elements = *driver*.findElements(By.*className*(
> "Submit-button-icon"));
>
> Thread.*sleep*(1000);
> System.*out*.println("Test number of elements: " + elements.size());
>
> WebElement button=elements.get(0);
>
> button.click();
>
> I need to click second element using "elements" array
>
> I used this:
>
> WebElement button1=elements.get(1);
>
> button1.click();
>
> But this is not working.Please help

What do you mean by “it's not working”? Are you getting an exception?
What happens if you locate the element directly and attempt to click it?
Are you sure you've located your element correctly?

Kandurke Vinayak

unread,
Jul 1, 2014, 7:55:53 AM7/1/14
to webd...@googlegroups.com
I am getting "Source not found" exception when I debug it




--
You received this message because you are subscribed to a topic in the Google Groups "webdriver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webdriver/QX4YRV-Fzjc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webdriver+unsubscribe@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

darrell

unread,
Jul 1, 2014, 8:28:56 AM7/1/14
to webd...@googlegroups.com
This is going to gather all the WebElements and return the list back to your bindings. This could be a lot of data being transmitted. By.className("Submit-button-icon") is actually the same as By.cssSelector(".Submit-button-icon"). If you want the second item from this list then you can use By.cssSelector(".Submit-button-icon:nth-of-type(2)"). In other words, trim the code down to:

    WebElement button = driver.findElement(By.cssSelector(".Submit-button-icon:nth-of-type(2)"));
    button.click();

Once you do that, set a breakpoint on the button.click(), run to breakpoint, then examine the element using the Watch Window, e.g. button.getAttribute() or button.getTagName() to see what you actually found. Maybe the second item isn't what you think it is. Or maybe you found the right element but there is something wrong with it and you found a legitimate defect.
Reply all
Reply to author
Forward
0 new messages