我曾尝试过多次在输入框中定位元素,但总是被告知出现了错误,因此失败了。我如何找到原因?
参数:Android,appium,python。
代码如下:
time.sleep(2)
self.driver.find_element_by_xpath("//*[@class='android.widget.EditText'and
@index='0']").send_keys("XXXXX")
self.driver.find_element_by_xpath("//*[@class='android.widget.EditText'][1]").send_keys("XXXXX")
self.driver.find_element_by_xpath("//*[@text='login']").click()
结果:
Error
Traceback (most recent call last):
File "C:\xxxx\AppData\Local\Programs\Python\Python36\lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\xxxx\AppData\Local\Programs\Python\Python36\lib\unittest\case.py", line 601, in run
testMethod()
File "D:\XXXXX\login.py", line 33, in testchangeto_pageone
self.driver.find_element_by_xpath("//*[@class='android.widget.EditText'and @index='0']").send_keys("xxxx")
File "C:\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\xxxx\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\xxxx\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\xxxxx\AppData\Local\Programs\Python\Python36\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
raise wde
File "C:\xxxx\AppData\Local\Programs\Python\Python36\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
super(MobileErrorHandler, self).check_response(response)
File "C:\xxxx\AppData\Local\Programs\Python\Python36\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 'xxxxxx'. Did you interact with the correct element?
您的xpath错误。供您参考,附加的图像也是错误的。在所附的屏幕快照中显示imageView时,您必须找到editText。
尝试在Xpath下使用。
self.driver.find_element_by_xpath("//android.widget.EditText[@index='0']").send_keys("XXXXX")
另一个文本框:
self.driver.find_element_by_xpath("//android.widget.EditText[@index='1']").send_keys("XXXXX")