Appium unable to get "content-desc" attribute data - java

In the below case, Appium is correctly able to locate elements by class, but when we want to manipulate the data based on each element's content-desc we see an error. Why can't we get attribute for content-desc?
Any advice appreciated.
List<WebElement> arrayOfProperties2 = driver.findElementsByClassName("android.view.View");
List<WebElement> propertyMarkerEle = new ArrayList<>();
System.out.println("Found arrayOfProperties2 total: "+ arrayOfProperties2.size());
for (WebElement property : arrayOfProperties2){
String contentDesc = property.getAttribute("content-desc");
if (contentDesc.contains("property"))
propertyMarkerEle.add(property);
Error: Found arrayOfProperties2 total: 32
org.openqa.selenium.NoSuchElementException: An element could not be
located on the page using the given search parameters. (WARNING: The
server did not provide any stacktrace information)

Use "name"
property.getAttribute("name");

The list of accepted attribute names can be found in boolean attrbutes and string attributes.
The boolean attributes are:
enabled
checkable
checked
clickable
focusable
focused
longClickable
scrollable
selected
displayed
The string attibutes are:
contentDescription
text
className
resourceId
I tested this using the python bindings.
Credit goes to TikhomirovSergey in a github comment.

try this:
driver.findElement(By.AccessibilityID(""));

text can be used as
driver.findElement(By.xpath("//*[#text='Remove Contact']"))
OR content-desc can be used as
driver.findElement(By.name("Remove Contact"))
driver.findElement(By.xpath("//*[#content-desc='Remove Contact']"))
driver.findElement(By.name("Remove Contact"))

Related

Find element by text inside another element using UISelector query

I have the following code snippet and the screenshot attached.
String query = "new UiScrollable(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\"))" +
".scrollIntoView(new UiSelector().text(\"Test Group\"))";
driver.findElementByAndroidUIAutomator (query).click ();
What I want is to find an element with the text "Test Group" using UISelector, but inside the RecyclerView only (not searching the whole app source). What I get is the element inside search field instead (not in the RecyclerView).
Please advice. I know that I can get all searched elements using findElements(By.id("name")). But I want to use UI selector in this case.
With UiSelector you can use chaining:
String query = "new UiScrollable(resourseIdMatches(\".*recycler_view\")).scrollIntoView(resourseIdMatches(\".*recycler_view\")).childSelector(text(\"Text Group\")))";
In addition new UiSelector... part can be omitted. Appium does support this syntax.

How to locate a button with a dynamicID

I am automating an Android app using Appium where we need to click a button with a dynamic ID. Either the button has ID "PROFILEBUTTON" or ID "PROFILEMAILBUTTON". Apart from co-ordinates, what else can be used to automate clicking this button?
You can use partial id
driver.findElement(By.cssSelector("[id*='PROFIL'][id*='BUTTON']"));
Or with xpath
driver.findElement(By.cssSelector("//*[contains(#id, 'PROFIL') and contains(#id, 'BUTTON')]"));
driver.findElement(By.cssSelector("//*[contains(#id, 'PROFIL')][contains(#id, 'BUTTON')]"));
To identify an element with dynamic ID either PROFILEBUTTON or PROFILEMAILBUTTON you can use cssSelector with the following wildcards :
^ : To indicate an attribute value starts with
$ : To indicate an attribute value ends with
So the most granular locator would include the strategy to lookout for the initial letters i.e. PROFILE and the ending letters i.e. BUTTON and should be :
driver.findElement(By.cssSelector("[id^='PROFILE'][id$='BUTTON']"));
Update
As per your comment update, you can use either of the equivalent xpath as follows :
driver.findElement(By.xpath("//*[contains(#resource-id,'profileMail') and contains(#resource-id,'Button')]"));
//or
driver.findElement(By.xpath("//*[contains(#resource-id,'profileMailButton') or contains(#resource-id,'profileMailPremiumButton')]"));
driver.findElement(By.xpath("//*[contains(#resource-id,'profileMailButton') or contains(#resource-id,'profileMailPremiumButton')]"));
This worked for me.

Appium iOS automation using Java: All Elements that have Accessibility ID

I am using Selenium 3.x with Appium and working on Native iOS App which uses XCUITest as Automation name.
I can access individual element with Accessibility ID and it works fine e.g. driver.findElement(MobileBy.AccessibilityId("Location")).click();
Is there way to get a list of all Accessiblilty IDs (or the values) on a single page / form?
Thanks in advance.
You can try something like below, only you have tochange first line of the code as per android schema, as I'm not aware where all the elements are contained in an app.
Following example identifies the name (or accessibility id attribute in your case) for all the div elements, so you just need to tweak the first line:
List<WebElement> allElems = driver.findElements(By.xpath("//body//div"));
System.out.println("Avaialable Accesible ids :");
int c=0;
for(WebElement ele : allElems) {
c++;
System.out.print("for element " + c +" Accessibility ID is :");
System.out.println(ele.getAttribute("name"));
//or
System.out.println(ele.getAttribute("content-desc"));
}

XPath- Getting Element from a table having dynamic ID

I'm trying to automate my Test Cases using Selenium for an OBIEE application. Now, I need to read a value from a tabular report generated. The problem is, the ID of the last cell where the total is, keeps on changing.
For example- Currently the id is: db_saw_9270_6_1610_0.
After refreshing, the ID becomes something else. The 4 numbers in between (9270) changes. The remaining bit are the same. I'm using the following logic to capture this element:
driver.findElement(By.xpath(".//*[contains(#id, '_6_1610_0')]")).getText();
But, it is returning org.openqa.selenium.NoSuchElementException: Unable to locate element:
Please tell me where did I go wrong and what should I do?
you can try starts-with and substring (as a substitute for xpath 2.0 methdod ends-with):
string xpath = "//*[starts-with(#id, 'db_saw_') and substring(#id, string-length(#id) - 8) = '_6_1610_0']"
driver.findElement(By.xpath(xpath)).getText();
You can try below xpath:-
driver.findElement(By.xpath("//*[starts-with(#id, 'db_saw')]")).getText();
driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();
Note: the a represents a html element. If your id is in a element then you have to replace a by table.
Check this out for more examples with css selector

How to Search using wild card entries in Java Selenium

I have gone through every related question but none of them seems to provide the correct solution. I am writing a program to fetch the friend list from Facebook using selenium code. To do so, I am using for loop to scroll page down and fetch friends name link text from it using tagname keyword. While doing so, I get mutual friends and number of friends each friend have, also in the result.
I noticed that each friend id starts with js_X where X can be any value.
I need to know how I can use any wild card search/Regular expression to search and fetch the results to me.
Below is my code(I know its poor programming, but please help me):
--code for opening facebook and reaching till Friendlist page here---
WebElement box = d.findElement(By.xpath(".//*[#id='pagelet_timeline_app_collection_100000641984658:2356318349:2']/ul")); **//This find the first box containing friend list**
List<WebElement> FinalList = box.findElements(By.tagName("a")); **//All Names are fetched and added in Final List**
jse.executeScript("scroll(0, 2500)"); **//Scrolled below to second box**
Thread.sleep(15000L);
box = d.findElement(By.xpath(".//*[#id='pagelet_timeline_app_collection_100000641984658:2356318349:2']/ul[2]"));**//This finds second box and fetches all the friends name from that box**
List<WebElement> IntermediateList; **//Temporarylist created**
for(int i=3,k=7; i<17||k<20; i++){
if(i<17){
box = d.findElement(By.xpath(".//*[#id='pagelet_timeline_app_collection_100000641984658:2356318349:2']/ul["+i+"]"));
if(box.isDisplayed()){
IntermediateList = box.findElements(By.tagName("a"));
FinalList.addAll(IntermediateList);
jse.executeScript("scroll(0, "+k+"000)"); **//This is for increasing scroll everytime as sometimes box is further down**
Thread.sleep(15000L);
k++;
}else{
break;
}
}
FinalList.addAll(IntermediateList);
Instead of tagname("a") I want to use "id" to find the friend name link text and use Regular expression/wild card in it to search.
For example : IntermediateList = box.findElements(By.id("js_(wild card parameter)"));
any Suggestion would help, Thanks in advance!!
You already used xpath in your example so why don't you use xpath to find the desired links?
XPATH:
IntermediateList = box.findElements(By.xpath("//*[contains(#id,'js_')]"));
(Find any element which ID contains js_)
CSS:
IntermediateList = box.findElements(By.cssSelector("a[id^='js_']"));
(Find a link <a href...> which ID starts with js_)

Categories

Resources