1

I have two elements in webpage with same linktext in same table structure. I have to ignore first element and select second element everytime if present. But problem is selenium selecting first element every time, How can I select second element instead of first element(ignore first element)?

I can't use anything other than Linktext to identify that element using selenium, That's the constraint.

4
  • is the values present in the table? if yes, then choose the particular cell locator value Commented Oct 7, 2016 at 8:52
  • 1
    Use driver.findElements(By......) which returns a list... if list size is 2, then take the second element. Else ignore based on size. Commented Oct 7, 2016 at 8:58
  • How to check list size and how to select second element of that list. Can you explain it in detail? Commented Oct 7, 2016 at 9:30
  • @Parth list.size() second element - list(1)... Just google for a java list tutorial. Commented Oct 7, 2016 at 9:35

2 Answers 2

3

when elements having same link text or having same loactors, there is one collection present in java i.e List create a list of webelements having such kind of scenario, then by there index you can access the elements.

List<WebElement> list1 = driver.findElements(By.linkText("Services"));
for(int i=0;i<list1.size();i++)
{
 System.out.println(i+" "+list1.getText());
 //this can be used incase number of elements is more and no time to count there index
}
list1.get(1).click();
Sign up to request clarification or add additional context in comments.

Comments

1

Here it is for getting second element via link text where I used collection class to store all same kind of elements.

List<WebElement> li = driver.findElements(By.linkText("Services"));;
li.get(1).click();

Comments

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.