0
votes

I'm using Appium client to record and generate the testing script for my iOS App. On the App Inspector, I can tap on a login button and generate the script (in python) like below:

els1 = driver.find_elements_by_accessibility_id("login")
els1[0].click()

I can successfully log in to my app tapping the button on the App Inspector yet I got an error as I run the script on mac terminal:

els3[0].click()

IndexError: list index out of range

I tried different ways to access the button element by using accessibility id, name and class name, but none of the above work.

What did I miss? Is it a bug of the Appium software?

2

2 Answers

0
votes

This error "IndexError: list index out of range" appears if we try to access list at index which is not present inside the list range

e.g.

thislist = ["apple", "banana", "cherry"] // here list index range is 0-2
thislist[1] = "blackcurrant" // this works fine as value of index is in range 0-2

But if I try below

// this is run time error i.e. "IndexError: list index out of range"
//  as value of index is out of range 0-2
    thislist[3] = "blackcurrant" 

Note: List Index starts with 0

Consider a case where find_elements_by_accessibility_id("login") method does not return any element for any reason

els1 = driver.find_elements_by_accessibility_id("login");

And I try to access List els1 at 0 index which is empty so I get error "IndexError: list index out of range"

Now before accessing List we will check if List in not empty

els1 = driver.find_elements_by_accessibility_id("login")
if els1:
   els1[0].click()
else :
   print "Element not found and els1 array is empty"        
0
votes

After hours of googling and trying, I've found that the problem is about view refreshing.

Every time a view transition or navigation happens, it takes time to update the view. Once everything is updated the webDriver could successfully identify an element using the given search parameters.

So between every interaction just wait for a second:

el1 = driver.find_element_by_accessibility_id("login")
el1.click()
// wait for the view to get updated
driver.implicitly_wait(1)

els2 = driver.find_elements_by_name("Edit")
els2[0].click()

And the script would run as expected.