使用Uiautomator语法定位元素时,报错此元素不是一个字符串 is not a string 【已解决】
import time from appium import webdriver des = { "platformName": "Android", "platformVersion": "8.1.0", "deviceName": "vivo x20", "appPackage": "com.ibox.calculators", "appActivity": "com.ibox.calculators.CalculatorActivity", "udid": "c6c8c4ce", "noReset": True, "unicodeKeyboard": True, "resetKeyboard": True, } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.find_element_by_android_uiautomator("text('9')").click()
Traceback (most recent call last): File "C:/Users/admin/PycharmProjects/Appiunm_UI/adb_demo1/demo_04hunhe.py", line 27, in <module> driver.find_element_by_android_uiautomator("text('9')").click() File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\extensions\search_context\android.py", line 147, in find_element_by_android_uiautomator return self.find_element(by=MobileBy.ANDROID_UIAUTOMATOR, value=uia_string) File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\webdriver.py", line 283, in find_element return self.execute(RemoteCommand.FIND_ELEMENT, {'using': by, 'value': value})['value'] File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\errorhandler.py", line 31, in check_response raise wde File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\errorhandler.py", line 26, in check_response super().check_response(response) File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: Could not parse selector expression `new UiSelector().text('9')`: `UiSelector` doesn't have suitable method `text` with arguments ['9']: Could not parse selector expression `new UiSelector().text('9')`: '9' is not a string
后来查阅资料发现
需要将里面的括号变成双引号,外面的括号变成单引号才能进行识别
import time from appium import webdriver des = { "platformName": "Android", "platformVersion": "8.1.0", "deviceName": "vivo x20", "appPackage": "com.ibox.calculators", "appActivity": "com.ibox.calculators.CalculatorActivity", "udid": "c6c8c4ce", "noReset": True, "unicodeKeyboard": True, "resetKeyboard": True, } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
# 单双引号惹的祸
driver.find_element_by_android_uiautomator("text('9')").click()
https://blog.csdn.net/qq544649790/article/details/83960311?ops_request_misc=&request_id=&biz_id=102&utm_term=UiSelector%20%E6%89%A7%E8%A1%8Cnew%20UiSelector().&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-6-83960311.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187