Search code examples
pythonseleniumappium

Return something that isn't element ID with driver.find_elements_by


I am looking for a list of certain elements of a certain class. However, driver.find_elements_by_locator returns a list of WebElements be default. Since the element ID is changing at every moment, I was wondering whether I could use find_elements_by_locator and return a list of something a bit more constant, like XPATH.

Code:

listofblocks = driver1.find_elements_by_class_name("android.widget.RelativeLayout")
print(listofblocks)

Nothing doesn't technically work, just looking for a better return.


Solution

  • See,

    driver.find_elements_by_locator 
    

    where locator could be xpath, css, or id, class.. etc.

    it will always return a list, not element IDs.

    and that list will contains WebElements associated with the locator passed.

    once you have a list, in your case listofblocks , you can iterate it :-

    for block in listofblocks:
        print(block.text) # this is just an example, you can call any webelement method here. 
    

    also

    listofblocks[0]
    

    would return first element of the list, so

    you can do

    listofblocks[0].click() or listofblocks[0].text

    and so on..