Parth
Parth

Reputation: 25

How to skip first element in webpage and choose second element using selenium

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.

Upvotes: 1

Views: 2543

Answers (2)

Renuka Deshpande
Renuka Deshpande

Reputation: 174

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();

Upvotes: 3

anurag0510
anurag0510

Reputation: 763

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();

Upvotes: 1

Related Questions