Error finding element using xpath, appium, python

Screenshot of UIAUTOMATORVIEWER

I am trying to write a python code to verify for the presence of .mp3 and corresponding .json file in the dropbox.

dropboxmp3txt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View/android.widget.TextView[contains(@index, "1")]')
print dropboxmp3txt.text

This gives me the output :

abc2016-05-05, 10.38.26.mp3`  

Which is perfectly fine!!!

I need the similar xpath to find the .json text. The one which is below the .mp3 , in the figure.

I tried doing :

dropboxmp3txt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[0]/android.widget.TextView[contains(@index, "1")]')
print dropboxmp3txt.text
dropboxjsontxt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[1]/android.widget.TextView[contains(@index, "2")]')
print dropboxjsontxt.text

But, that gave me an error :

Traceback (most recent call last):
  File "dropboxshare.py", line 136, in test_1_view_checkbox
    dropboxtxt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[0]/android.widget.TextView[contains(@index, "1")]')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 258, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/appium/webdriver/errorhandler.py", line 29, in check_response
    raise wde
NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

There is no ID specified for the 2 texts… Only thing that is unique in both is the extension (.mp3 and .json)…

How can I retrieve these 2 texts ??

ANY IDEA ???

Try it out :-
xpath("//android.widget.ListView/android.view.View/android.widget.TextView[@index=‘1’ and @text=‘abc2016-05-05, 10.38.26.mp3’ ]");

and for the other element :-
xpath("//android.widget.ListView/android.view.View/android.widget.TextView[@index=‘1’ and @text=‘abc2016-05-05, 10.38.26.json’ ]");
It should works, as you mentioned that names are unique.

Let me know if it worked or not !

@jitenderbhardwaj
The texts
abc2016-05-5,10.38.26.mp3
and
abc2016-05-5,10.38.26.json
I can’t enter directly… Some other .mp3 and .json might come during the next run and occupy those place…
It changes during runtime… I need to know what text is present in that location first.
I have to retrieve it…
Is there a way, where i can assign the name (abc2016-05-05, 10.38.26) to a variable and then put that variable into xpath ??

Eg:
variable_name = ‘abc2016-05-05, 10.38.26.json’
dropboxjsontxt = self.driver.find_element_by_xpath("//android.widget.ListView/android.view.View[1]/android.widget.TextView[contains(@index=‘1’ and @text= variable_name ]")

Can we do something like this ??

are you taking about this? :-
driver.findElement(By.className(“android.view.xxxx”)).getText();

class name is same for the textview (android.widget.TextView ) within the listview…
Does that retrieve both the text and assign it to seperate variables ??

first, try to print to see what exactly you are getting using this command

I am trying it using using python…
I have tried… it retrieves only the name of .mp3… not the .json…

Can you share this app details(PlayStore Link) with me?

I tried the same scenario on hike using java to retreiver data.
And this code works for me :-

List <WebElement> counter = driver.findElements(By.id("com.bsb.hike:id/contact"));
for(int index= 0 ;  index < counter.size() ;index++){
System.out.println(counter.get(index).getText());}

I want to do it using python…
link to Dropbox app https://play.google.com/store/apps/details?id=com.dropbox.android&hl=en

i got a work around as of now… i am retrieving all the textview elements using xpath,
then i am storing the required string in a variable and comparing it with all…

Xpath children are indexed starting from 1, not 0. If you want the second child, you want to pass in 2, not 1.

@afwang Sorry that was a typo…
I have passed in index 1 and 2 only…

dropboxmp3txt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[1]/android.widget.TextView[contains(@index, "1")]')
print dropboxmp3txt.text

dropboxjsontxt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[2]/android.widget.TextView[contains(@index, "1")]')
print dropboxjsontxt.text

Output is :
abc2016-05-5,10.38.26.mp3
abc2016-05-5,10.38.26.mp3

I got the same .mp3 twice… I din’t get the .json…

@afwang

Thanks a lot…

dropboxmp3txt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[1]/android.widget.TextView[contains(@index, "1")]')
print dropboxmp3txt.text
dropboxjsontxt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[2]/android.widget.TextView[contains(@index, "1")]')
print dropboxjsontxt.text

This solved the problem…
Got the output :
abc2016-05-5,10.38.26.mp3
abc2016-05-5,10.38.26.json

Its the same code i tried previously… don’t know how i got correct result this time…
strange!!!

I think I remember seeing your reply, and I think you didn’t add the index on the View element that’s immediately under the ListView. When you index your elements with ListView, you want to use the index on the child immediately under the ListView.

In simpler terms, instead of this:

//android.widget.ListView/android.view.View/android.widget.TextView[...]

you want this:

//android.widget.ListView/android.view.View[2]/android.widget.TextView[...]

can someone help me with this :

1 Like