Search code examples
pythonappium

return self.find_element (by = By.ID, value = id_) and exception errors in Python appium


I want to write automation test in python. I'm using an android studio emulator and appium. I want to write automation for the calculator, but somewhere there has been an error.

I added time.sleep() but the problem still continues.

from appium import webdriver

import time

caps = {"deviceName": "emulator-5554", "platformName": "android", "appPackage": "com.android.calculator2",
        "appActivity": ".Calculator", "noReset": True}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)

el2 = driver.find_element_by_id("com.android.calculator2:id/digit_8")

el2.click()

time.sleep(2)

el3 = driver.find_element_by_accessibility_id("times")

el3.click()

time.sleep(2)

el4 = driver.find_element_by_id("com.android.calculator2:id/digit_3")

el4.click()

time.sleep(2)

el5 = driver.find_element_by_accessibility_id("equals")

el5.click()

time.sleep(2)

el6 = driver.find_element_by_id("com.android.calculator2:id/formula")

el6.click()

time.sleep(2)

driver.quit()

I'm waiting for the android emulator to open and calculate.

I have this error lines;

> C:\python37\dersler1\venv\Scripts\python.exe
> C:/python37/dersler1/hs.py Traceback (most recent call last):   File
> "C:/python37/dersler1/hs.py", line 12, in <module>
>     el2 = driver.find_element_by_id("com.android.calculator2:id/digit_8")   File
> "C:\python37\dersler1\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
> line 360, in find_element_by_id
>     return self.find_element(by=By.ID, value=id_)   File "C:\python37\dersler1\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
> line 978, in find_element
>     'value': value})['value']   File "C:\python37\dersler1\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
> line 321, in execute
>     self.error_handler.check_response(response)   File "C:\python37\dersler1\venv\lib\site-packages\appium\webdriver\errorhandler.py",
> line 29, in check_response
>     raise wde   File "C:\python37\dersler1\venv\lib\site-packages\appium\webdriver\errorhandler.py",
> line 24, in check_response
>     super(MobileErrorHandler, self).check_response(response)   File "C:\python37\dersler1\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
> line 242, in check_response
>     raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: Locator
> Strategy 'css selector' is not supported for this session

Solution

  • import os
    from appium import webdriver
    import time
    
    PATH = lambda p: os.path.abspath(
        os.path.join(os.path.dirname(__file__), p))
    desired_caps = {
        "deviceName": "emulator-5554",
        "platformName": "android",
        "appPackage": "com.android.calculator2",
        "appActivity": ".Calculator",
        "noReset": True
    }
    driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
    
    while True:
        try:
            el1 = driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.view.ViewGroup[1]/android.widget.Button[6]')
            el1.click()
            time.sleep(1)
            el2 = driver.find_element_by_accessibility_id("times")
            el2.click()
            time.sleep(1)
            el3 = driver.find_element_by_xpath( 
    '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.view.ViewGroup[1]/android.widget.Button[9]')
            el3.click()
            time.sleep(1)
            el4 = driver.find_element_by_accessibility_id("equals")
            el4.click()
            time.sleep(1)
        except ValueError:
            print(" ERROR BRO  ")
            pass
        break
    

    Some ids are working, others are not. I used xpath instead of id and the error improved. I think the number of ids is constantly changing. Xpath helped.