appium遇到定位元素不能输入的情况,selenium.common.exceptions.InvalidElementStateException: Message: Cannot set the element to '111111'. Did you interact with the correct element?【已解决】
遇到元素无法定位的情况
先查看定位的元素,使用sendkeys会报错
driver.find_element(By.XPATH, '//android.widget.RelativeLayout[4]/android.widget.LinearLayout/android.widget.TextView').click() time.sleep(1) driver.find_element(By.XPATH, '//android.view.View[@text="登陆/注册"]').click() time.sleep(2) phone = driver.find_element(By.XPATH, '//android.webkit.WebView/android.view.View[2]/android.view.View/android.view.View[@text="输入手机号码"]') phone.send_keys("111111")
报错信息
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/Yaoweilai_UI/login_suite/test_login.py", line 34, in <module>
phone.send_keys("111111")
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\webelement.py", line 218, in send_keys
self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, {'text': ''.join(keys), 'value': keys})
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
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.InvalidElementStateException: Message: Cannot set the element to '111111'. Did you interact with the correct element?
Process finished with exit code 1
分析一下定位的元素,是否支持点击,发现是View不能进行输入操作
由于输入框控件是android.view.View,不是android.widget.EditText,所以只能点击,不能sendkeys发送内容,可以用 driver.press_keycode(8)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) driver.find_element(By.XPATH, '//android.widget.RelativeLayout[4]/android.widget.LinearLayout/android.widget.TextView').click() time.sleep(1) driver.find_element(By.XPATH, '//android.view.View[@text="登陆/注册"]').click() time.sleep(2) phone = driver.find_element(By.XPATH, '//android.webkit.WebView/android.view.View[2]/android.view.View/android.view.View[@text="输入手机号码"]') phone.send_keys("111111") phone.click() time.sleep(1) driver.press_keycode(8).press_keycode(12).press_keycode(12).press_keycode(14)
解决
https://blog.csdn.net/lwdfzr/article/details/104017394?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163401818516780264080176%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163401818516780264080176&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-104017394.first_rank_v2_pc_rank_v29&utm_term=Call+to+%27sendKeys%27+failed+%5Belement.sendKeys%28%221%22%29%5D+Error+response+status%3A+12%2C+InvalidElementState+-+An+element+command+could+not+be+completed+because+the+element+is+in+an+invalid+state+%28e.g.+attempting&spm=1018.2226.3001.4187