Finding elements by class name


#1

Hi All,

I am trying to select something from a list using class name as ID isn’t available.

The problem I have is that on this page there are 7 options. They all have the same class name! “android.widget.RelativeLayout”. I noticed they have an index that goes in order for 1 to 7.

Currently I have this:

WebElement anotherproblem = driver.findElement(By.className(“android.widget.RelativeLayout”));

While it works it doesn’t click the specific option on the page I want. Is there a way I can tell it to click #1 in the list?

Thanks!


#2

Try to build xpath with class name and any other locators


#3

The inspect element gives me an xpath just trying to avoid using that since it’s very long!! Also I believe xpath is the worst one to use.


#4

Can you try like this?

WebElement a= driver.findElement(By.className(“android.widget.RelativeLayout”));
a.findElements(By.tagName(“android.widget.RelativeLayout”)).get(0).click();

and update if any issue


#5

I tried that and I got the error:

Locator Strategy ‘tag name’ is not supported for this session

Trying to find a solution for this as it seems like it will work if I can get it supported!


#6

WebElement element = (WebElement) driver.findElements(By.className(“android.widget.RelativeLayout”)).get(1);
element.click();


#7

@Manish_Boricha This has worked thank you very much!! My tests thank you! :smiley:


#8

My Pleasure…:slight_smile: